Skip to content

Commit 088cca2

Browse files
ustcweizhouRohit Yadav
authored andcommitted
CLOUDSTACK-9997: Add cpu cores information in CapacityResponse
This commit contains following changes (1) add CPU CORE information in op_host_capacity (2) add capacity name in the CapacityResponse (3) add allocatedCapacity for CPU/MEMORY/CPU CORE for zones (4) sort CapacityResponse by zonename and CapacityType
1 parent 8299657 commit 088cca2

11 files changed

Lines changed: 207 additions & 5 deletions

File tree

api/src/com/cloud/capacity/Capacity.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ public interface Capacity extends InternalIdentity, Identity {
3232
public static final short CAPACITY_TYPE_LOCAL_STORAGE = 9;
3333
public static final short CAPACITY_TYPE_GPU = 19;
3434

35+
public static final short CAPACITY_TYPE_CPU_CORE = 90;
36+
3537
public Long getHostOrPoolId();
3638

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

5153
public Float getUsedPercentage();
54+
55+
public Long getAllocatedCapacity();
5256
}

api/src/org/apache/cloudstack/api/command/admin/resource/ListCapacityCmd.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package org.apache.cloudstack.api.command.admin.resource;
1818

1919
import java.text.DecimalFormat;
20+
import java.util.Collections;
21+
import java.util.Comparator;
2022
import java.util.List;
2123

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

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

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

128131
ListResponse<CapacityResponse> response = new ListResponse<CapacityResponse>();
129132
List<CapacityResponse> capacityResponses = _responseGenerator.createCapacityResponse(result, s_percentFormat);
133+
Collections.sort(capacityResponses, new Comparator<CapacityResponse>() {
134+
public int compare(CapacityResponse resp1, CapacityResponse resp2) {
135+
int res = resp1.getZoneName().compareTo(resp2.getZoneName());
136+
if (res != 0) {
137+
return res;
138+
} else {
139+
return resp1.getCapacityType().compareTo(resp2.getCapacityType());
140+
}
141+
}
142+
});
143+
130144
response.setResponses(capacityResponses);
131145
response.setResponseName(getCommandName());
132146
this.setResponseObject(response);

api/src/org/apache/cloudstack/api/response/CapacityResponse.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ public class CapacityResponse extends BaseResponse {
2828
@Param(description = "the capacity type")
2929
private Short capacityType;
3030

31+
@SerializedName(ApiConstants.NAME)
32+
@Param(description="the capacity name")
33+
private String capacityName;
34+
3135
@SerializedName(ApiConstants.ZONE_ID)
3236
@Param(description = "the Zone ID")
3337
private String zoneId;
@@ -52,6 +56,10 @@ public class CapacityResponse extends BaseResponse {
5256
@Param(description = "the Cluster name")
5357
private String clusterName;
5458

59+
@SerializedName("capacityallocated")
60+
@Param(description="the capacity currently in allocated")
61+
private Long capacityAllocated;
62+
5563
@SerializedName("capacityused")
5664
@Param(description = "the capacity currently in use")
5765
private Long capacityUsed;
@@ -72,6 +80,14 @@ public void setCapacityType(Short capacityType) {
7280
this.capacityType = capacityType;
7381
}
7482

83+
public String getCapacityName() {
84+
return capacityName;
85+
}
86+
87+
public void setCapacityName(String capacityName) {
88+
this.capacityName = capacityName;
89+
}
90+
7591
public String getZoneId() {
7692
return zoneId;
7793
}
@@ -120,6 +136,14 @@ public void setClusterName(String clusterName) {
120136
this.clusterName = clusterName;
121137
}
122138

139+
public Long getCapacityAllocated() {
140+
return capacityAllocated;
141+
}
142+
143+
public void setCapacityAllocated(Long capacityAllocated) {
144+
this.capacityAllocated = capacityAllocated;
145+
}
146+
123147
public Long getCapacityUsed() {
124148
return capacityUsed;
125149
}

engine/schema/src/com/cloud/capacity/CapacityVO.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package com.cloud.capacity;
1818

1919
import java.util.Date;
20+
import java.util.HashMap;
21+
import java.util.Map;
2022

2123
import javax.persistence.Column;
2224
import javax.persistence.Entity;
@@ -75,6 +77,9 @@ public class CapacityVO implements Capacity {
7577
@Transient
7678
private Float usedPercentage;
7779

80+
@Transient
81+
private Long allocatedCapacity;
82+
7883
public CapacityVO() {
7984
}
8085

@@ -208,8 +213,37 @@ public void setUsedPercentage(float usedPercentage) {
208213
this.usedPercentage = usedPercentage;
209214
}
210215

216+
public Long getAllocatedCapacity() {
217+
return allocatedCapacity;
218+
}
219+
220+
public void setAllocatedCapacity(Long allocatedCapacity) {
221+
this.allocatedCapacity = allocatedCapacity;
222+
}
223+
211224
@Override
212225
public String getUuid() {
213226
return null; //To change body of implemented methods use File | Settings | File Templates.
214227
}
228+
229+
private static Map<Short, String> capacityNames = null;
230+
static {
231+
capacityNames = new HashMap<Short, String>();
232+
capacityNames.put(CAPACITY_TYPE_MEMORY, "MEMORY");
233+
capacityNames.put(CAPACITY_TYPE_CPU, "CPU");
234+
capacityNames.put(CAPACITY_TYPE_STORAGE, "STORAGE");
235+
capacityNames.put(CAPACITY_TYPE_STORAGE_ALLOCATED, "STORAGE_ALLOCATED");
236+
capacityNames.put(CAPACITY_TYPE_VIRTUAL_NETWORK_PUBLIC_IP, "VIRTUAL_NETWORK_PUBLIC_IP");
237+
capacityNames.put(CAPACITY_TYPE_PRIVATE_IP, "PRIVATE_IP");
238+
capacityNames.put(CAPACITY_TYPE_SECONDARY_STORAGE, "SECONDARY_STORAGE");
239+
capacityNames.put(CAPACITY_TYPE_VLAN, "VLAN");
240+
capacityNames.put(CAPACITY_TYPE_DIRECT_ATTACHED_PUBLIC_IP, "DIRECT_ATTACHED_PUBLIC_IP");
241+
capacityNames.put(CAPACITY_TYPE_LOCAL_STORAGE, "LOCAL_STORAGE");
242+
capacityNames.put(CAPACITY_TYPE_GPU, "GPU");
243+
capacityNames.put(CAPACITY_TYPE_CPU_CORE, "CPU_CORE");
244+
}
245+
246+
public static String getCapacityName (Short capacityType) {
247+
return capacityNames.get(capacityType);
248+
}
215249
}

engine/schema/src/com/cloud/capacity/dao/CapacityDaoImpl.java

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,17 @@ public class CapacityDaoImpl extends GenericDaoBase<CapacityVO, Long> implements
187187
+
188188
"from op_host_capacity capacity where cluster_id = ? and capacity_type = ?;";
189189

190+
private static final String LIST_ALLOCATED_CAPACITY_GROUP_BY_CAPACITY_AND_ZONE = "SELECT v.data_center_id, SUM(cpu) AS cpucore, " +
191+
"SUM(cpu * speed) AS cpu, SUM(ram_size * 1024 * 1024) AS memory " +
192+
"FROM (SELECT vi.data_center_id, (CASE WHEN ISNULL(service_offering.cpu) THEN custom_cpu.value ELSE service_offering.cpu end) AS cpu, " +
193+
"(CASE WHEN ISNULL(service_offering.speed) THEN custom_speed.value ELSE service_offering.speed end) AS speed, " +
194+
"(CASE WHEN ISNULL(service_offering.ram_size) THEN custom_ram_size.value ELSE service_offering.ram_size end) AS ram_size " +
195+
"FROM (((vm_instance vi LEFT JOIN service_offering ON(((vi.service_offering_id = service_offering.id))) " +
196+
"LEFT JOIN user_vm_details custom_cpu ON(((custom_cpu.vm_id = vi.id) AND (custom_cpu.name = 'CpuNumber')))) " +
197+
"LEFT JOIN user_vm_details custom_speed ON(((custom_speed.vm_id = vi.id) AND (custom_speed.name = 'CpuSpeed')))) " +
198+
"LEFT JOIN user_vm_details custom_ram_size ON(((custom_ram_size.vm_id = vi.id) AND (custom_ram_size.name = 'memory')))) " +
199+
"WHERE ISNULL(vi.removed) AND vi.state NOT IN ('Destroyed', 'Error', 'Expunging')";
200+
190201
public CapacityDaoImpl() {
191202
_hostIdTypeSearch = createSearchBuilder();
192203
_hostIdTypeSearch.and("hostId", _hostIdTypeSearch.entity().getHostOrPoolId(), SearchCriteria.Op.EQ);
@@ -407,6 +418,33 @@ public List<SummedCapacity> findCapacityBy(Integer capacityType, Long zoneId, Lo
407418
PreparedStatement pstmt = null;
408419
List<SummedCapacity> results = new ArrayList<SummedCapacity>();
409420

421+
StringBuilder allocatedSql = new StringBuilder(LIST_ALLOCATED_CAPACITY_GROUP_BY_CAPACITY_AND_ZONE);
422+
423+
HashMap<Long, Long> sumCpuCore = new HashMap<Long, Long>();
424+
HashMap<Long, Long> sumCpu = new HashMap<Long, Long>();
425+
HashMap<Long, Long> sumMemory = new HashMap<Long, Long>();
426+
if (zoneId != null){
427+
allocatedSql.append(" AND vi.data_center_id = ?");
428+
}
429+
allocatedSql.append(" ) AS v GROUP BY v.data_center_id");
430+
try {
431+
if (podId == null && clusterId == null) {
432+
// add allocated capacity of zone in result
433+
pstmt = txn.prepareAutoCloseStatement(allocatedSql.toString());
434+
if (zoneId != null){
435+
pstmt.setLong(1, zoneId);
436+
}
437+
ResultSet rs = pstmt.executeQuery();
438+
if (rs.next()) {
439+
sumCpuCore.put(rs.getLong(1), rs.getLong(2));
440+
sumCpu.put(rs.getLong(1), rs.getLong(3));
441+
sumMemory.put(rs.getLong(1), rs.getLong(4));
442+
}
443+
}
444+
} catch (SQLException e) {
445+
throw new CloudRuntimeException("DB Exception on: " + allocatedSql, e);
446+
}
447+
410448
StringBuilder sql = new StringBuilder(LIST_CAPACITY_GROUP_BY_CAPACITY_PART1);
411449
List<Long> resourceIdList = new ArrayList<Long>();
412450

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

430-
sql.append(LIST_CAPACITY_GROUP_BY_CAPACITY_DATA_CENTER_POD_CLUSTER);
468+
if (podId == null && clusterId == null) {
469+
sql.append(" GROUP BY capacity_type, data_center_id");
470+
} else {
471+
sql.append(LIST_CAPACITY_GROUP_BY_CAPACITY_DATA_CENTER_POD_CLUSTER);
472+
}
431473

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

483+
Long capacityZoneId = rs.getLong(6);
441484
Long capacityPodId = null;
442485
Long capacityClusterId = null;
443486

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

496+
if (podId == null && clusterId == null) {
497+
Short sumCapacityType = summedCapacity.getCapacityType();
498+
if (sumCapacityType == CapacityVO.CAPACITY_TYPE_MEMORY) {
499+
summedCapacity.setAllocatedCapacity(sumMemory.get(capacityZoneId));
500+
} else if (sumCapacityType == CapacityVO.CAPACITY_TYPE_CPU) {
501+
summedCapacity.setAllocatedCapacity(sumCpu.get(capacityZoneId));
502+
} else if (sumCapacityType == CapacityVO.CAPACITY_TYPE_CPU_CORE) {
503+
summedCapacity.setAllocatedCapacity(sumCpuCore.get(capacityZoneId));
504+
}
505+
}
453506
results.add(summedCapacity);
454507
}
455508
HashMap<String, SummedCapacity> capacityMap = new HashMap<String, SummedCapacity>();
@@ -460,7 +513,7 @@ public List<SummedCapacity> findCapacityBy(Integer capacityType, Long zoneId, Lo
460513
}
461514
else {
462515
// sum the values based on the zoneId.
463-
key=String.valueOf(result.getDataCenterId())+String.valueOf(result.getCapacityType());
516+
key=String.valueOf(result.getDataCenterId()) + "-" + String.valueOf(result.getCapacityType());
464517
}
465518
SummedCapacity tempCapacity=null;
466519
if (capacityMap.containsKey(key)) {
@@ -589,6 +642,7 @@ public List<Long> listHostsWithEnoughCapacity(int requiredCpu, long requiredRam,
589642
}
590643

591644
public static class SummedCapacity {
645+
public Long sumAllocated;
592646
public long sumUsed;
593647
public long sumReserved;
594648
public long sumTotal;
@@ -679,6 +733,12 @@ public void setPodId(Long podId) {
679733
public void setClusterId(Long clusterId) {
680734
this.clusterId=clusterId;
681735
}
736+
public Long getAllocatedCapacity() {
737+
return sumAllocated;
738+
}
739+
public void setAllocatedCapacity(Long sumAllocated) {
740+
this.sumAllocated = sumAllocated;
741+
}
682742
}
683743

684744
@Override

server/src/com/cloud/api/ApiResponseHelper.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -958,6 +958,7 @@ public PodResponse createPodResponse(Pod pod, Boolean showCapacities) {
958958
for (SummedCapacity capacity : capacities) {
959959
CapacityResponse capacityResponse = new CapacityResponse();
960960
capacityResponse.setCapacityType(capacity.getCapacityType());
961+
capacityResponse.setCapacityName(CapacityVO.getCapacityName(capacity.getCapacityType()));
961962
capacityResponse.setCapacityUsed(capacity.getUsedCapacity() + capacity.getReservedCapacity());
962963
if (capacity.getCapacityType() == Capacity.CAPACITY_TYPE_STORAGE_ALLOCATED) {
963964
List<SummedCapacity> c = ApiDBUtils.findNonSharedStorageForClusterPodZone(null, pod.getId(), null);
@@ -994,6 +995,7 @@ public static List<CapacityResponse> getDataCenterCapacityResponse(Long zoneId)
994995
for (SummedCapacity capacity : capacities) {
995996
CapacityResponse capacityResponse = new CapacityResponse();
996997
capacityResponse.setCapacityType(capacity.getCapacityType());
998+
capacityResponse.setCapacityName(CapacityVO.getCapacityName(capacity.getCapacityType()));
997999
capacityResponse.setCapacityUsed(capacity.getUsedCapacity() + capacity.getReservedCapacity());
9981000
if (capacity.getCapacityType() == Capacity.CAPACITY_TYPE_STORAGE_ALLOCATED) {
9991001
List<SummedCapacity> c = ApiDBUtils.findNonSharedStorageForClusterPodZone(zoneId, null, null);
@@ -1026,6 +1028,7 @@ private static List<CapacityResponse> getStatsCapacityresponse(Long poolId, Long
10261028
for (CapacityVO capacity : capacities) {
10271029
CapacityResponse capacityResponse = new CapacityResponse();
10281030
capacityResponse.setCapacityType(capacity.getCapacityType());
1031+
capacityResponse.setCapacityName(CapacityVO.getCapacityName(capacity.getCapacityType()));
10291032
capacityResponse.setCapacityUsed(capacity.getUsedCapacity());
10301033
capacityResponse.setCapacityTotal(capacity.getTotalCapacity());
10311034
if (capacityResponse.getCapacityTotal() != 0) {
@@ -1110,6 +1113,7 @@ public ClusterResponse createClusterResponse(Cluster cluster, Boolean showCapaci
11101113
for (SummedCapacity capacity : capacities) {
11111114
CapacityResponse capacityResponse = new CapacityResponse();
11121115
capacityResponse.setCapacityType(capacity.getCapacityType());
1116+
capacityResponse.setCapacityName(CapacityVO.getCapacityName(capacity.getCapacityType()));
11131117
capacityResponse.setCapacityUsed(capacity.getUsedCapacity() + capacity.getReservedCapacity());
11141118

11151119
if (capacity.getCapacityType() == Capacity.CAPACITY_TYPE_STORAGE_ALLOCATED) {
@@ -1606,7 +1610,11 @@ public List<CapacityResponse> createCapacityResponse(List<? extends Capacity> re
16061610
for (Capacity summedCapacity : result) {
16071611
CapacityResponse capacityResponse = new CapacityResponse();
16081612
capacityResponse.setCapacityTotal(summedCapacity.getTotalCapacity());
1613+
if (summedCapacity.getAllocatedCapacity() != null) {
1614+
capacityResponse.setCapacityAllocated(summedCapacity.getAllocatedCapacity());
1615+
}
16091616
capacityResponse.setCapacityType(summedCapacity.getCapacityType());
1617+
capacityResponse.setCapacityName(CapacityVO.getCapacityName(summedCapacity.getCapacityType()));
16101618
capacityResponse.setCapacityUsed(summedCapacity.getUsedCapacity());
16111619
if (summedCapacity.getPodId() != null) {
16121620
capacityResponse.setPodId(ApiDBUtils.findPodById(summedCapacity.getPodId()).getUuid());
@@ -1677,6 +1685,7 @@ public List<CapacityResponse> createCapacityResponse(List<? extends Capacity> re
16771685
capacityResponse.setClusterName(cluster.getName());
16781686
}
16791687
capacityResponse.setCapacityType(Capacity.CAPACITY_TYPE_GPU);
1688+
capacityResponse.setCapacityName(CapacityVO.getCapacityName(Capacity.CAPACITY_TYPE_GPU));
16801689
capacityResponse.setCapacityUsed((long)Math.ceil(capacityUsed));
16811690
capacityResponse.setCapacityTotal(capacityMax);
16821691
if (capacityMax > 0) {

0 commit comments

Comments
 (0)