Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLOUDSTACK-9997: Add cpu cores information in CapacityResponse #2179

Merged
merged 1 commit into from
Jul 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions api/src/com/cloud/capacity/Capacity.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public interface Capacity extends InternalIdentity, Identity {
public static final short CAPACITY_TYPE_LOCAL_STORAGE = 9;
public static final short CAPACITY_TYPE_GPU = 19;

public static final short CAPACITY_TYPE_CPU_CORE = 90;

public Long getHostOrPoolId();

public Long getDataCenterId();
Expand All @@ -49,4 +51,6 @@ public interface Capacity extends InternalIdentity, Identity {
public long getReservedCapacity();

public Float getUsedPercentage();

public Long getAllocatedCapacity();
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package org.apache.cloudstack.api.command.admin.resource;

import java.text.DecimalFormat;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.apache.log4j.Logger;
Expand Down Expand Up @@ -65,7 +67,8 @@ public class ListCapacityCmd extends BaseListCmd {

@Parameter(name = ApiConstants.TYPE, type = CommandType.INTEGER, description = "lists capacity by type" + "* CAPACITY_TYPE_MEMORY = 0" + "* CAPACITY_TYPE_CPU = 1"
+ "* CAPACITY_TYPE_STORAGE = 2" + "* CAPACITY_TYPE_STORAGE_ALLOCATED = 3" + "* CAPACITY_TYPE_VIRTUAL_NETWORK_PUBLIC_IP = 4" + "* CAPACITY_TYPE_PRIVATE_IP = 5"
+ "* CAPACITY_TYPE_SECONDARY_STORAGE = 6" + "* CAPACITY_TYPE_VLAN = 7" + "* CAPACITY_TYPE_DIRECT_ATTACHED_PUBLIC_IP = 8" + "* CAPACITY_TYPE_LOCAL_STORAGE = 9.")
+ "* CAPACITY_TYPE_SECONDARY_STORAGE = 6" + "* CAPACITY_TYPE_VLAN = 7" + "* CAPACITY_TYPE_DIRECT_ATTACHED_PUBLIC_IP = 8" + "* CAPACITY_TYPE_LOCAL_STORAGE = 9"
+ "* CAPACITY_TYPE_GPU = 19" + "* CAPACITY_TYPE_CPU_CORE = 90.")
private Integer type;

@Parameter(name = ApiConstants.SORT_BY, type = CommandType.STRING, since = "3.0.0", description = "Sort the results. Available values: Usage")
Expand Down Expand Up @@ -127,6 +130,17 @@ public void execute() {

ListResponse<CapacityResponse> response = new ListResponse<CapacityResponse>();
List<CapacityResponse> capacityResponses = _responseGenerator.createCapacityResponse(result, s_percentFormat);
Collections.sort(capacityResponses, new Comparator<CapacityResponse>() {
public int compare(CapacityResponse resp1, CapacityResponse resp2) {
int res = resp1.getZoneName().compareTo(resp2.getZoneName());
if (res != 0) {
return res;
} else {
return resp1.getCapacityType().compareTo(resp2.getCapacityType());
}
}
});

response.setResponses(capacityResponses);
response.setResponseName(getCommandName());
this.setResponseObject(response);
Expand Down
24 changes: 24 additions & 0 deletions api/src/org/apache/cloudstack/api/response/CapacityResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ public class CapacityResponse extends BaseResponse {
@Param(description = "the capacity type")
private Short capacityType;

@SerializedName(ApiConstants.NAME)
@Param(description="the capacity name")
private String capacityName;

@SerializedName(ApiConstants.ZONE_ID)
@Param(description = "the Zone ID")
private String zoneId;
Expand All @@ -52,6 +56,10 @@ public class CapacityResponse extends BaseResponse {
@Param(description = "the Cluster name")
private String clusterName;

@SerializedName("capacityallocated")
@Param(description="the capacity currently in allocated")
private Long capacityAllocated;

@SerializedName("capacityused")
@Param(description = "the capacity currently in use")
private Long capacityUsed;
Expand All @@ -72,6 +80,14 @@ public void setCapacityType(Short capacityType) {
this.capacityType = capacityType;
}

public String getCapacityName() {
return capacityName;
}

public void setCapacityName(String capacityName) {
this.capacityName = capacityName;
}

public String getZoneId() {
return zoneId;
}
Expand Down Expand Up @@ -120,6 +136,14 @@ public void setClusterName(String clusterName) {
this.clusterName = clusterName;
}

public Long getCapacityAllocated() {
return capacityAllocated;
}

public void setCapacityAllocated(Long capacityAllocated) {
this.capacityAllocated = capacityAllocated;
}

public Long getCapacityUsed() {
return capacityUsed;
}
Expand Down
34 changes: 34 additions & 0 deletions engine/schema/src/com/cloud/capacity/CapacityVO.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package com.cloud.capacity;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.persistence.Column;
import javax.persistence.Entity;
Expand Down Expand Up @@ -75,6 +77,9 @@ public class CapacityVO implements Capacity {
@Transient
private Float usedPercentage;

@Transient
private Long allocatedCapacity;

public CapacityVO() {
}

Expand Down Expand Up @@ -208,8 +213,37 @@ public void setUsedPercentage(float usedPercentage) {
this.usedPercentage = usedPercentage;
}

public Long getAllocatedCapacity() {
return allocatedCapacity;
}

public void setAllocatedCapacity(Long allocatedCapacity) {
this.allocatedCapacity = allocatedCapacity;
}

@Override
public String getUuid() {
return null; //To change body of implemented methods use File | Settings | File Templates.
}

private static Map<Short, String> capacityNames = null;
static {
capacityNames = new HashMap<Short, String>();
capacityNames.put(CAPACITY_TYPE_MEMORY, "MEMORY");
capacityNames.put(CAPACITY_TYPE_CPU, "CPU");
capacityNames.put(CAPACITY_TYPE_STORAGE, "STORAGE");
capacityNames.put(CAPACITY_TYPE_STORAGE_ALLOCATED, "STORAGE_ALLOCATED");
capacityNames.put(CAPACITY_TYPE_VIRTUAL_NETWORK_PUBLIC_IP, "VIRTUAL_NETWORK_PUBLIC_IP");
capacityNames.put(CAPACITY_TYPE_PRIVATE_IP, "PRIVATE_IP");
capacityNames.put(CAPACITY_TYPE_SECONDARY_STORAGE, "SECONDARY_STORAGE");
capacityNames.put(CAPACITY_TYPE_VLAN, "VLAN");
capacityNames.put(CAPACITY_TYPE_DIRECT_ATTACHED_PUBLIC_IP, "DIRECT_ATTACHED_PUBLIC_IP");
capacityNames.put(CAPACITY_TYPE_LOCAL_STORAGE, "LOCAL_STORAGE");
capacityNames.put(CAPACITY_TYPE_GPU, "GPU");
capacityNames.put(CAPACITY_TYPE_CPU_CORE, "CPU_CORE");
}

public static String getCapacityName (Short capacityType) {
return capacityNames.get(capacityType);
}
}
64 changes: 62 additions & 2 deletions engine/schema/src/com/cloud/capacity/dao/CapacityDaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,17 @@ public class CapacityDaoImpl extends GenericDaoBase<CapacityVO, Long> implements
+
"from op_host_capacity capacity where cluster_id = ? and capacity_type = ?;";

private static final String LIST_ALLOCATED_CAPACITY_GROUP_BY_CAPACITY_AND_ZONE = "SELECT v.data_center_id, SUM(cpu) AS cpucore, " +
"SUM(cpu * speed) AS cpu, SUM(ram_size * 1024 * 1024) AS memory " +
"FROM (SELECT vi.data_center_id, (CASE WHEN ISNULL(service_offering.cpu) THEN custom_cpu.value ELSE service_offering.cpu end) AS cpu, " +
"(CASE WHEN ISNULL(service_offering.speed) THEN custom_speed.value ELSE service_offering.speed end) AS speed, " +
"(CASE WHEN ISNULL(service_offering.ram_size) THEN custom_ram_size.value ELSE service_offering.ram_size end) AS ram_size " +
"FROM (((vm_instance vi LEFT JOIN service_offering ON(((vi.service_offering_id = service_offering.id))) " +
"LEFT JOIN user_vm_details custom_cpu ON(((custom_cpu.vm_id = vi.id) AND (custom_cpu.name = 'CpuNumber')))) " +
"LEFT JOIN user_vm_details custom_speed ON(((custom_speed.vm_id = vi.id) AND (custom_speed.name = 'CpuSpeed')))) " +
"LEFT JOIN user_vm_details custom_ram_size ON(((custom_ram_size.vm_id = vi.id) AND (custom_ram_size.name = 'memory')))) " +
"WHERE ISNULL(vi.removed) AND vi.state NOT IN ('Destroyed', 'Error', 'Expunging')";

public CapacityDaoImpl() {
_hostIdTypeSearch = createSearchBuilder();
_hostIdTypeSearch.and("hostId", _hostIdTypeSearch.entity().getHostOrPoolId(), SearchCriteria.Op.EQ);
Expand Down Expand Up @@ -407,6 +418,33 @@ public List<SummedCapacity> findCapacityBy(Integer capacityType, Long zoneId, Lo
PreparedStatement pstmt = null;
List<SummedCapacity> results = new ArrayList<SummedCapacity>();

StringBuilder allocatedSql = new StringBuilder(LIST_ALLOCATED_CAPACITY_GROUP_BY_CAPACITY_AND_ZONE);

HashMap<Long, Long> sumCpuCore = new HashMap<Long, Long>();
HashMap<Long, Long> sumCpu = new HashMap<Long, Long>();
HashMap<Long, Long> sumMemory = new HashMap<Long, Long>();
if (zoneId != null){
allocatedSql.append(" AND vi.data_center_id = ?");
}
allocatedSql.append(" ) AS v GROUP BY v.data_center_id");
try {
if (podId == null && clusterId == null) {
// add allocated capacity of zone in result
pstmt = txn.prepareAutoCloseStatement(allocatedSql.toString());
if (zoneId != null){
pstmt.setLong(1, zoneId);
}
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
sumCpuCore.put(rs.getLong(1), rs.getLong(2));
sumCpu.put(rs.getLong(1), rs.getLong(3));
sumMemory.put(rs.getLong(1), rs.getLong(4));
}
}
} catch (SQLException e) {
throw new CloudRuntimeException("DB Exception on: " + allocatedSql, e);
}

StringBuilder sql = new StringBuilder(LIST_CAPACITY_GROUP_BY_CAPACITY_PART1);
List<Long> resourceIdList = new ArrayList<Long>();

Expand All @@ -427,7 +465,11 @@ public List<SummedCapacity> findCapacityBy(Integer capacityType, Long zoneId, Lo
resourceIdList.add(capacityType.longValue());
}

sql.append(LIST_CAPACITY_GROUP_BY_CAPACITY_DATA_CENTER_POD_CLUSTER);
if (podId == null && clusterId == null) {
sql.append(" GROUP BY capacity_type, data_center_id");
} else {
sql.append(LIST_CAPACITY_GROUP_BY_CAPACITY_DATA_CENTER_POD_CLUSTER);
}

try {
pstmt = txn.prepareAutoCloseStatement(sql.toString());
Expand All @@ -438,6 +480,7 @@ public List<SummedCapacity> findCapacityBy(Integer capacityType, Long zoneId, Lo
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {

Long capacityZoneId = rs.getLong(6);
Long capacityPodId = null;
Long capacityClusterId = null;

Expand All @@ -450,6 +493,16 @@ public List<SummedCapacity> findCapacityBy(Integer capacityType, Long zoneId, Lo
(short)rs.getLong(5), rs.getLong(6),
capacityPodId, capacityClusterId);

if (podId == null && clusterId == null) {
Short sumCapacityType = summedCapacity.getCapacityType();
if (sumCapacityType == CapacityVO.CAPACITY_TYPE_MEMORY) {
summedCapacity.setAllocatedCapacity(sumMemory.get(capacityZoneId));
} else if (sumCapacityType == CapacityVO.CAPACITY_TYPE_CPU) {
summedCapacity.setAllocatedCapacity(sumCpu.get(capacityZoneId));
} else if (sumCapacityType == CapacityVO.CAPACITY_TYPE_CPU_CORE) {
summedCapacity.setAllocatedCapacity(sumCpuCore.get(capacityZoneId));
}
}
results.add(summedCapacity);
}
HashMap<String, SummedCapacity> capacityMap = new HashMap<String, SummedCapacity>();
Expand All @@ -460,7 +513,7 @@ public List<SummedCapacity> findCapacityBy(Integer capacityType, Long zoneId, Lo
}
else {
// sum the values based on the zoneId.
key=String.valueOf(result.getDataCenterId())+String.valueOf(result.getCapacityType());
key=String.valueOf(result.getDataCenterId()) + "-" + String.valueOf(result.getCapacityType());
}
SummedCapacity tempCapacity=null;
if (capacityMap.containsKey(key)) {
Expand Down Expand Up @@ -589,6 +642,7 @@ public List<Long> listHostsWithEnoughCapacity(int requiredCpu, long requiredRam,
}

public static class SummedCapacity {
public Long sumAllocated;
public long sumUsed;
public long sumReserved;
public long sumTotal;
Expand Down Expand Up @@ -679,6 +733,12 @@ public void setPodId(Long podId) {
public void setClusterId(Long clusterId) {
this.clusterId=clusterId;
}
public Long getAllocatedCapacity() {
return sumAllocated;
}
public void setAllocatedCapacity(Long sumAllocated) {
this.sumAllocated = sumAllocated;
}
}

@Override
Expand Down
9 changes: 9 additions & 0 deletions server/src/com/cloud/api/ApiResponseHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -958,6 +958,7 @@ public PodResponse createPodResponse(Pod pod, Boolean showCapacities) {
for (SummedCapacity capacity : capacities) {
CapacityResponse capacityResponse = new CapacityResponse();
capacityResponse.setCapacityType(capacity.getCapacityType());
capacityResponse.setCapacityName(CapacityVO.getCapacityName(capacity.getCapacityType()));
capacityResponse.setCapacityUsed(capacity.getUsedCapacity() + capacity.getReservedCapacity());
if (capacity.getCapacityType() == Capacity.CAPACITY_TYPE_STORAGE_ALLOCATED) {
List<SummedCapacity> c = ApiDBUtils.findNonSharedStorageForClusterPodZone(null, pod.getId(), null);
Expand Down Expand Up @@ -994,6 +995,7 @@ public static List<CapacityResponse> getDataCenterCapacityResponse(Long zoneId)
for (SummedCapacity capacity : capacities) {
CapacityResponse capacityResponse = new CapacityResponse();
capacityResponse.setCapacityType(capacity.getCapacityType());
capacityResponse.setCapacityName(CapacityVO.getCapacityName(capacity.getCapacityType()));
capacityResponse.setCapacityUsed(capacity.getUsedCapacity() + capacity.getReservedCapacity());
if (capacity.getCapacityType() == Capacity.CAPACITY_TYPE_STORAGE_ALLOCATED) {
List<SummedCapacity> c = ApiDBUtils.findNonSharedStorageForClusterPodZone(zoneId, null, null);
Expand Down Expand Up @@ -1026,6 +1028,7 @@ private static List<CapacityResponse> getStatsCapacityresponse(Long poolId, Long
for (CapacityVO capacity : capacities) {
CapacityResponse capacityResponse = new CapacityResponse();
capacityResponse.setCapacityType(capacity.getCapacityType());
capacityResponse.setCapacityName(CapacityVO.getCapacityName(capacity.getCapacityType()));
capacityResponse.setCapacityUsed(capacity.getUsedCapacity());
capacityResponse.setCapacityTotal(capacity.getTotalCapacity());
if (capacityResponse.getCapacityTotal() != 0) {
Expand Down Expand Up @@ -1110,6 +1113,7 @@ public ClusterResponse createClusterResponse(Cluster cluster, Boolean showCapaci
for (SummedCapacity capacity : capacities) {
CapacityResponse capacityResponse = new CapacityResponse();
capacityResponse.setCapacityType(capacity.getCapacityType());
capacityResponse.setCapacityName(CapacityVO.getCapacityName(capacity.getCapacityType()));
capacityResponse.setCapacityUsed(capacity.getUsedCapacity() + capacity.getReservedCapacity());

if (capacity.getCapacityType() == Capacity.CAPACITY_TYPE_STORAGE_ALLOCATED) {
Expand Down Expand Up @@ -1608,7 +1612,11 @@ public List<CapacityResponse> createCapacityResponse(List<? extends Capacity> re
for (Capacity summedCapacity : result) {
CapacityResponse capacityResponse = new CapacityResponse();
capacityResponse.setCapacityTotal(summedCapacity.getTotalCapacity());
if (summedCapacity.getAllocatedCapacity() != null) {
capacityResponse.setCapacityAllocated(summedCapacity.getAllocatedCapacity());
}
capacityResponse.setCapacityType(summedCapacity.getCapacityType());
capacityResponse.setCapacityName(CapacityVO.getCapacityName(summedCapacity.getCapacityType()));
capacityResponse.setCapacityUsed(summedCapacity.getUsedCapacity());
if (summedCapacity.getPodId() != null) {
capacityResponse.setPodId(ApiDBUtils.findPodById(summedCapacity.getPodId()).getUuid());
Expand Down Expand Up @@ -1679,6 +1687,7 @@ public List<CapacityResponse> createCapacityResponse(List<? extends Capacity> re
capacityResponse.setClusterName(cluster.getName());
}
capacityResponse.setCapacityType(Capacity.CAPACITY_TYPE_GPU);
capacityResponse.setCapacityName(CapacityVO.getCapacityName(Capacity.CAPACITY_TYPE_GPU));
capacityResponse.setCapacityUsed((long)Math.ceil(capacityUsed));
capacityResponse.setCapacityTotal(capacityMax);
if (capacityMax > 0) {
Expand Down