You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudstack.apache.org by se...@apache.org on 2014/06/10 09:12:39 UTC

git commit: updated refs/heads/4.4-forward to a1f278e

Repository: cloudstack
Updated Branches:
  refs/heads/4.4-forward a4b401f29 -> a1f278e9d


CLOUDSTACK-6850: Return cpu cores, cpu speed and memory in listUsageRecords

Signed-off-by: Sebastien Goasguen <ru...@gmail.com>


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

Branch: refs/heads/4.4-forward
Commit: a1f278e9d47e8029d58df6d3aae13495965ca434
Parents: a4b401f
Author: Olivier Lemasle <ol...@apalia.net>
Authored: Sun Jun 8 17:59:17 2014 +0200
Committer: Sebastien Goasguen <ru...@gmail.com>
Committed: Tue Jun 10 03:12:29 2014 -0400

----------------------------------------------------------------------
 .../api/response/UsageRecordResponse.java       | 24 +++++++++
 api/src/org/apache/cloudstack/usage/Usage.java  |  6 +++
 .../src/com/cloud/usage/UsageVMInstanceVO.java  | 17 ++++++
 engine/schema/src/com/cloud/usage/UsageVO.java  | 46 ++++++++++++++++
 .../cloud/usage/dao/UsageVMInstanceDaoImpl.java | 28 +++++++---
 server/src/com/cloud/api/ApiResponseHelper.java |  4 ++
 setup/db/db/schema-430to440.sql                 |  5 ++
 .../usage/parser/VMInstanceUsageParser.java     | 57 +++++++++++++-------
 8 files changed, 161 insertions(+), 26 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cloudstack/blob/a1f278e9/api/src/org/apache/cloudstack/api/response/UsageRecordResponse.java
----------------------------------------------------------------------
diff --git a/api/src/org/apache/cloudstack/api/response/UsageRecordResponse.java b/api/src/org/apache/cloudstack/api/response/UsageRecordResponse.java
index 5e2e85d..87a085c 100644
--- a/api/src/org/apache/cloudstack/api/response/UsageRecordResponse.java
+++ b/api/src/org/apache/cloudstack/api/response/UsageRecordResponse.java
@@ -101,6 +101,18 @@ public class UsageRecordResponse extends BaseResponse implements ControlledEntit
     @Param(description = "virtual size of resource")
     private Long virtualSize;
 
+    @SerializedName(ApiConstants.CPU_NUMBER)
+    @Param(description = "number of cpu of resource")
+    private Long cpuNumber;
+
+    @SerializedName(ApiConstants.CPU_SPEED)
+    @Param(description = "speed of each cpu of resource")
+    private Long cpuSpeed;
+
+    @SerializedName(ApiConstants.MEMORY)
+    @Param(description = "memory allocated for the resource")
+    private Long memory;
+
     @SerializedName(ApiConstants.START_DATE)
     @Param(description = "start date of the usage record")
     private String startDate;
@@ -229,4 +241,16 @@ public class UsageRecordResponse extends BaseResponse implements ControlledEntit
     public void setVirtualSize(Long virtualSize) {
         this.virtualSize = virtualSize;
     }
+
+    public void setCpuNumber(Long cpuNumber) {
+        this.cpuNumber = cpuNumber;
+    }
+
+    public void setCpuSpeed(Long cpuSpeed) {
+        this.cpuSpeed = cpuSpeed;
+    }
+
+    public void setMemory(Long memory) {
+        this.memory = memory;
+    }
 }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/a1f278e9/api/src/org/apache/cloudstack/usage/Usage.java
----------------------------------------------------------------------
diff --git a/api/src/org/apache/cloudstack/usage/Usage.java b/api/src/org/apache/cloudstack/usage/Usage.java
index 20dc189..fe35390 100644
--- a/api/src/org/apache/cloudstack/usage/Usage.java
+++ b/api/src/org/apache/cloudstack/usage/Usage.java
@@ -40,6 +40,12 @@ public interface Usage {
 
     public String getVmName();
 
+    public Long getCpuCores();
+
+    public Long getCpuSpeed();
+
+    public Long getMemory();
+
     public Long getOfferingId();
 
     public Long getTemplateId();

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/a1f278e9/engine/schema/src/com/cloud/usage/UsageVMInstanceVO.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/usage/UsageVMInstanceVO.java b/engine/schema/src/com/cloud/usage/UsageVMInstanceVO.java
index 06d4876..1e7b424 100644
--- a/engine/schema/src/com/cloud/usage/UsageVMInstanceVO.java
+++ b/engine/schema/src/com/cloud/usage/UsageVMInstanceVO.java
@@ -86,6 +86,23 @@ public class UsageVMInstanceVO {
         this.endDate = endDate;
     }
 
+    public UsageVMInstanceVO(int usageType, long zoneId, long accountId, long vmInstanceId, String vmName, long serviceOfferingId, long templateId,
+            Long cpuSpeed, Long cpuCores, Long memory, String hypervisorType, Date startDate, Date endDate) {
+        this.usageType = usageType;
+        this.zoneId = zoneId;
+        this.accountId = accountId;
+        this.vmInstanceId = vmInstanceId;
+        this.vmName = vmName;
+        this.serviceOfferingId = serviceOfferingId;
+        this.templateId = templateId;
+        this.cpuSpeed = cpuSpeed;
+        this.cpuCores = cpuCores;
+        this.memory = memory;
+        this.hypervisorType = hypervisorType;
+        this.startDate = startDate;
+        this.endDate = endDate;
+    }
+
     public int getUsageType() {
         return usageType;
     }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/a1f278e9/engine/schema/src/com/cloud/usage/UsageVO.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/usage/UsageVO.java b/engine/schema/src/com/cloud/usage/UsageVO.java
index 67014ef..c46abb3 100644
--- a/engine/schema/src/com/cloud/usage/UsageVO.java
+++ b/engine/schema/src/com/cloud/usage/UsageVO.java
@@ -65,6 +65,15 @@ public class UsageVO implements Usage, InternalIdentity {
     @Column(name = "vm_name")
     private String vmName = null;
 
+    @Column(name = "cpu_cores")
+    private Long cpuCores = null;
+
+    @Column(name = "memory")
+    private Long memory = null;
+
+    @Column(name = "cpu_speed")
+    private Long cpuSpeed = null;
+
     @Column(name = "offering_id")
     private Long offeringId = null;
 
@@ -171,6 +180,28 @@ public class UsageVO implements Usage, InternalIdentity {
         this.endDate = endDate;
     }
 
+    public UsageVO(Long zoneId, Long accountId, Long domainId, String description, String usageDisplay, int usageType, Double rawUsage, Long vmId, String vmName,
+            Long cpuCores, Long cpuSpeed, Long memory, Long offeringId, Long templateId, Long usageId, Date startDate, Date endDate, String type) {
+        this.zoneId = zoneId;
+        this.accountId = accountId;
+        this.domainId = domainId;
+        this.description = description;
+        this.usageDisplay = usageDisplay;
+        this.usageType = usageType;
+        this.rawUsage = rawUsage;
+        this.vmInstanceId = vmId;
+        this.vmName = vmName;
+        this.cpuCores = cpuCores;
+        this.cpuSpeed = cpuSpeed;
+        this.memory = memory;
+        this.offeringId = offeringId;
+        this.templateId = templateId;
+        this.usageId = usageId;
+        this.type = type;
+        this.startDate = startDate;
+        this.endDate = endDate;
+    }
+
     //IPAddress Usage
     public UsageVO(Long zoneId, Long accountId, Long domainId, String description, String usageDisplay, int usageType, Double rawUsage, Long usageId, long size,
             String type, Date startDate, Date endDate) {
@@ -239,6 +270,21 @@ public class UsageVO implements Usage, InternalIdentity {
     }
 
     @Override
+    public Long getCpuCores() {
+        return cpuCores;
+    }
+
+    @Override
+    public Long getCpuSpeed() {
+        return cpuSpeed;
+    }
+
+    @Override
+    public Long getMemory() {
+        return memory;
+    }
+
+    @Override
     public Long getOfferingId() {
         return offeringId;
     }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/a1f278e9/engine/schema/src/com/cloud/usage/dao/UsageVMInstanceDaoImpl.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/usage/dao/UsageVMInstanceDaoImpl.java b/engine/schema/src/com/cloud/usage/dao/UsageVMInstanceDaoImpl.java
index b94e12f..930ad89 100644
--- a/engine/schema/src/com/cloud/usage/dao/UsageVMInstanceDaoImpl.java
+++ b/engine/schema/src/com/cloud/usage/dao/UsageVMInstanceDaoImpl.java
@@ -42,8 +42,8 @@ public class UsageVMInstanceDaoImpl extends GenericDaoBase<UsageVMInstanceVO, Lo
         + "WHERE account_id = ? and vm_instance_id = ? and usage_type = ? and end_date IS NULL";
     protected static final String DELETE_USAGE_INSTANCE_SQL = "DELETE FROM usage_vm_instance WHERE account_id = ? and vm_instance_id = ? and usage_type = ?";
     protected static final String GET_USAGE_RECORDS_BY_ACCOUNT =
-        "SELECT usage_type, zone_id, account_id, vm_instance_id, vm_name, service_offering_id, template_id, hypervisor_type, start_date, end_date "
-            + "FROM usage_vm_instance " + "WHERE account_id = ? AND ((end_date IS NULL) OR (start_date BETWEEN ? AND ?) OR "
+        "SELECT usage_type, zone_id, account_id, vm_instance_id, vm_name, cpu_speed, cpu_cores, memory, service_offering_id, template_id, hypervisor_type, start_date, end_date "
+            + "FROM usage_vm_instance WHERE account_id = ? AND ((end_date IS NULL) OR (start_date BETWEEN ? AND ?) OR "
             + "      (end_date BETWEEN ? AND ?) OR ((start_date <= ?) AND (end_date >= ?)))";
 
     public UsageVMInstanceDaoImpl() {
@@ -113,11 +113,23 @@ public class UsageVMInstanceDaoImpl extends GenericDaoBase<UsageVMInstanceVO, Lo
                 long r_accountId = rs.getLong(3);
                 long r_vmId = rs.getLong(4);
                 String r_vmName = rs.getString(5);
-                long r_soId = rs.getLong(6);
-                long r_tId = rs.getLong(7);
-                String hypervisorType = rs.getString(8);
-                String r_startDate = rs.getString(9);
-                String r_endDate = rs.getString(10);
+                Long r_cpuSpeed = rs.getLong(6);
+                if (rs.wasNull()) {
+                    r_cpuSpeed = null;
+                }
+                Long r_cpuCores = rs.getLong(7);
+                if (rs.wasNull()) {
+                    r_cpuCores = null;
+                }
+                Long r_memory = rs.getLong(8);
+                if (rs.wasNull()) {
+                    r_memory = null;
+                }
+                long r_soId = rs.getLong(9);
+                long r_tId = rs.getLong(10);
+                String hypervisorType = rs.getString(11);
+                String r_startDate = rs.getString(12);
+                String r_endDate = rs.getString(13);
                 Date instanceStartDate = null;
                 Date instanceEndDate = null;
                 if (r_startDate != null) {
@@ -127,7 +139,7 @@ public class UsageVMInstanceDaoImpl extends GenericDaoBase<UsageVMInstanceVO, Lo
                     instanceEndDate = DateUtil.parseDateString(s_gmtTimeZone, r_endDate);
                 }
                 UsageVMInstanceVO usageInstance =
-                    new UsageVMInstanceVO(r_usageType, r_zoneId, r_accountId, r_vmId, r_vmName, r_soId, r_tId, hypervisorType, instanceStartDate, instanceEndDate);
+                    new UsageVMInstanceVO(r_usageType, r_zoneId, r_accountId, r_vmId, r_vmName, r_soId, r_tId, r_cpuSpeed, r_cpuCores, r_memory, hypervisorType, instanceStartDate, instanceEndDate);
                 usageInstances.add(usageInstance);
             }
         } catch (Exception ex) {

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/a1f278e9/server/src/com/cloud/api/ApiResponseHelper.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/api/ApiResponseHelper.java b/server/src/com/cloud/api/ApiResponseHelper.java
index 119f56d..a47f62d 100755
--- a/server/src/com/cloud/api/ApiResponseHelper.java
+++ b/server/src/com/cloud/api/ApiResponseHelper.java
@@ -3306,6 +3306,10 @@ public class ApiResponseHelper implements ResponseGenerator {
             }
             //Hypervisor Type
             usageRecResponse.setType(usageRecord.getType());
+            //Dynamic compute offerings details
+            usageRecResponse.setCpuNumber(usageRecord.getCpuCores());
+            usageRecResponse.setCpuSpeed(usageRecord.getCpuSpeed());
+            usageRecResponse.setMemory(usageRecord.getMemory());
 
         } else if (usageRecord.getUsageType() == UsageTypes.IP_ADDRESS) {
             //isSourceNAT

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/a1f278e9/setup/db/db/schema-430to440.sql
----------------------------------------------------------------------
diff --git a/setup/db/db/schema-430to440.sql b/setup/db/db/schema-430to440.sql
index adcd833..150d072 100644
--- a/setup/db/db/schema-430to440.sql
+++ b/setup/db/db/schema-430to440.sql
@@ -461,6 +461,11 @@ CREATE VIEW `cloud`.`user_vm_view` AS
            left join
         `cloud`.`user_vm_details` `custom_ram_size`  ON (((`custom_ram_size`.`vm_id` = `cloud`.`vm_instance`.`id`) and (`custom_ram_size`.`name` = 'memory')));
 
+ALTER TABLE `cloud_usage`.`cloud_usage` 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';
+
+
 -- ACL DB schema
 CREATE TABLE `cloud`.`iam_group` (
   `id` bigint unsigned NOT NULL UNIQUE auto_increment,

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/a1f278e9/usage/src/com/cloud/usage/parser/VMInstanceUsageParser.java
----------------------------------------------------------------------
diff --git a/usage/src/com/cloud/usage/parser/VMInstanceUsageParser.java b/usage/src/com/cloud/usage/parser/VMInstanceUsageParser.java
index ba0d4bf..684412f 100644
--- a/usage/src/com/cloud/usage/parser/VMInstanceUsageParser.java
+++ b/usage/src/com/cloud/usage/parser/VMInstanceUsageParser.java
@@ -27,7 +27,6 @@ import javax.inject.Inject;
 
 import org.apache.log4j.Logger;
 import org.springframework.stereotype.Component;
-
 import org.apache.cloudstack.usage.UsageTypes;
 
 import com.cloud.usage.UsageVMInstanceVO;
@@ -36,6 +35,7 @@ import com.cloud.usage.dao.UsageDao;
 import com.cloud.usage.dao.UsageVMInstanceDao;
 import com.cloud.user.AccountVO;
 import com.cloud.utils.Pair;
+import com.cloud.utils.StringUtils;
 
 @Component
 public class VMInstanceUsageParser {
@@ -75,7 +75,7 @@ public class VMInstanceUsageParser {
         Map<String, Pair<String, Long>> usageVMUptimeMap = new HashMap<String, Pair<String, Long>>();
         Map<String, Pair<String, Long>> allocatedVMMap = new HashMap<String, Pair<String, Long>>();
 
-        Map<String, VMInfo> vmServiceOfferingMap = new HashMap<String, VMInfo>();
+        Map<String, VMInfo> vmInfosMap = new HashMap<String, VMInfo>();
 
         // loop through all the usage instances, create a usage record for each
         for (UsageVMInstanceVO usageInstance : usageInstances) {
@@ -84,10 +84,13 @@ public class VMInstanceUsageParser {
             long zoneId = usageInstance.getZoneId();
             long tId = usageInstance.getTemplateId();
             int usageType = usageInstance.getUsageType();
-            String key = vmId + "-" + soId + "-" + usageType;
+            Long cpuCores = usageInstance.getCpuCores();
+            Long cpuSpeed = usageInstance.getCpuSpeed();
+            Long memory = usageInstance.getMemory();
+            String key = StringUtils.join("-", vmId, soId, usageType, cpuCores, cpuSpeed, memory);
 
-            // store the info in the service offering map
-            vmServiceOfferingMap.put(key, new VMInfo(vmId, zoneId, soId, tId, usageInstance.getHypervisorType()));
+            // store the info in the VMs map
+            vmInfosMap.put(key, new VMInfo(vmId, zoneId, soId, tId, usageInstance.getHypervisorType(), cpuCores, cpuSpeed, memory));
 
             Date vmStartDate = usageInstance.getStartDate();
             Date vmEndDate = usageInstance.getEndDate();
@@ -119,9 +122,9 @@ public class VMInstanceUsageParser {
 
             // Only create a usage record if we have a runningTime of bigger than zero.
             if (runningTime > 0L) {
-                VMInfo info = vmServiceOfferingMap.get(vmIdKey);
+                VMInfo info = vmInfosMap.get(vmIdKey);
                 createUsageRecord(UsageTypes.RUNNING_VM, runningTime, startDate, endDate, account, info.getVirtualMachineId(), vmUptimeInfo.first(), info.getZoneId(),
-                    info.getServiceOfferingId(), info.getTemplateId(), info.getHypervisorType());
+                    info.getServiceOfferingId(), info.getTemplateId(), info.getHypervisorType(), info.getCpuCores(), info.getCpuSpeed(), info.getMemory());
             }
         }
 
@@ -131,9 +134,9 @@ public class VMInstanceUsageParser {
 
             // Only create a usage record if we have a runningTime of bigger than zero.
             if (allocatedTime > 0L) {
-                VMInfo info = vmServiceOfferingMap.get(vmIdKey);
+                VMInfo info = vmInfosMap.get(vmIdKey);
                 createUsageRecord(UsageTypes.ALLOCATED_VM, allocatedTime, startDate, endDate, account, info.getVirtualMachineId(), vmAllocInfo.first(), info.getZoneId(),
-                    info.getServiceOfferingId(), info.getTemplateId(), info.getHypervisorType());
+                    info.getServiceOfferingId(), info.getTemplateId(), info.getHypervisorType(), info.getCpuCores(), info.getCpuSpeed(), info.getMemory());
             }
         }
 
@@ -153,7 +156,7 @@ public class VMInstanceUsageParser {
     }
 
     private static void createUsageRecord(int type, long runningTime, Date startDate, Date endDate, AccountVO account, long vmId, String vmName, long zoneId,
-        long serviceOfferingId, long templateId, String hypervisorType) {
+        long serviceOfferingId, long templateId, String hypervisorType, Long cpuCores, Long cpuSpeed, Long memory) {
         // Our smallest increment is hourly for now
         if (s_logger.isDebugEnabled()) {
             s_logger.debug("Total running time " + runningTime + "ms");
@@ -179,23 +182,29 @@ public class VMInstanceUsageParser {
         usageDesc += " (ServiceOffering: " + serviceOfferingId + ") (Template: " + templateId + ")";
         UsageVO usageRecord =
             new UsageVO(Long.valueOf(zoneId), account.getId(), account.getDomainId(), usageDesc, usageDisplay + " Hrs", type, new Double(usage), Long.valueOf(vmId),
-                vmName, Long.valueOf(serviceOfferingId), Long.valueOf(templateId), Long.valueOf(vmId), startDate, endDate, hypervisorType);
+                vmName, cpuCores, cpuSpeed, memory, Long.valueOf(serviceOfferingId), Long.valueOf(templateId), Long.valueOf(vmId), startDate, endDate, hypervisorType);
         s_usageDao.persist(usageRecord);
     }
 
     private static class VMInfo {
-        private long virtualMachineId;
-        private long zoneId;
-        private long serviceOfferingId;
-        private long templateId;
-        private String hypervisorType;
-
-        public VMInfo(long vmId, long zId, long soId, long tId, String hypervisorType) {
+        private final long virtualMachineId;
+        private final long zoneId;
+        private final long serviceOfferingId;
+        private final long templateId;
+        private final String hypervisorType;
+        private final Long cpuCores;
+        private final Long cpuSpeed;
+        private final Long memory;
+
+        public VMInfo(long vmId, long zId, long soId, long tId, String hypervisorType, Long cpuCores, Long cpuSpeed, Long memory) {
             virtualMachineId = vmId;
             zoneId = zId;
             serviceOfferingId = soId;
             templateId = tId;
             this.hypervisorType = hypervisorType;
+            this.cpuCores = cpuCores;
+            this.cpuSpeed = cpuSpeed;
+            this.memory = memory;
         }
 
         public long getZoneId() {
@@ -217,5 +226,17 @@ public class VMInstanceUsageParser {
         private String getHypervisorType() {
             return hypervisorType;
         }
+
+        public Long getCpuCores() {
+            return cpuCores;
+        }
+
+        public Long getCpuSpeed() {
+            return cpuSpeed;
+        }
+
+        public Long getMemory() {
+            return memory;
+        }
     }
 }