Skip to content

Commit d74a06a

Browse files
committed
temp commit
1 parent a29a6e2 commit d74a06a

13 files changed

Lines changed: 283 additions & 243 deletions

File tree

api/src/main/java/org/apache/cloudstack/api/command/admin/cluster/GenerateClusterDrsPlanCmd.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
import com.cloud.host.Host;
2323
import com.cloud.user.Account;
24-
import com.cloud.utils.Pair;
24+
import com.cloud.utils.Ternary;
2525
import com.cloud.vm.VirtualMachine;
2626
import org.apache.cloudstack.acl.RoleType;
2727
import org.apache.cloudstack.api.APICommand;
@@ -69,10 +69,10 @@ public Double getIterations() {
6969

7070
@Override
7171
public void execute() {
72-
final List<Pair<Host, VirtualMachine>> plan = clusterDrsService.generateDrsPlan(this);
72+
final List<Ternary<VirtualMachine, Host, Host>> plan = clusterDrsService.generateDrsPlan(this);
7373
final List<DrsPlanResponse> responseList = new ArrayList<>();
74-
for (Pair<Host, VirtualMachine> pair : plan) {
75-
final DrsPlanResponse response = new DrsPlanResponse(pair.first().getUuid(), pair.second().getUuid());
74+
for (Ternary<VirtualMachine, Host, Host> ternary : plan) {
75+
final DrsPlanResponse response = new DrsPlanResponse(ternary.first().getUuid(), ternary.second().getUuid(), ternary.third().getUuid());
7676
response.setObjectName("drsplan");
7777
response.setResponseName(getCommandName());
7878
responseList.add(response);

api/src/main/java/org/apache/cloudstack/api/command/admin/vm/MigrateMultipleVMsCmd.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,15 @@ public class MigrateMultipleVMsCmd extends BaseAsyncCmd {
5252
//////////////// API parameters /////////////////////
5353
/////////////////////////////////////////////////////
5454

55-
@Parameter(name = ApiConstants.HOST_ID,
55+
@Parameter(name = ApiConstants.HOST_IDS,
5656
type = CommandType.LIST,
5757
collectionType = CommandType.UUID,
5858
entityType = HostResponse.class,
5959
required = true,
6060
description = "Destination Host ID to migrate VM to.")
6161
private List<Long> hostIdList;
6262

63-
@Parameter(name = ApiConstants.VIRTUAL_MACHINE_ID,
63+
@Parameter(name = ApiConstants.VIRTUAL_MACHINE_IDS,
6464
type = CommandType.LIST,
6565
collectionType = CommandType.UUID,
6666
entityType = UserVmResponse.class,

api/src/main/java/org/apache/cloudstack/api/response/DrsPlanResponse.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,21 @@
2525
import org.apache.cloudstack.api.BaseResponse;
2626

2727
public class DrsPlanResponse extends BaseResponse {
28-
@SerializedName(ApiConstants.HOST_ID)
28+
@SerializedName("destinationhostid")
2929
@Param(description = "POST url to upload the file to")
30-
String hostId;
30+
String destHostId;
31+
32+
@SerializedName("sourcehostid")
33+
@Param(description = "POST url to upload the file to")
34+
String srcHostId;
3135

3236
@SerializedName(ApiConstants.VIRTUAL_MACHINE_ID)
3337
@Param(description = "POST url to upload the file to")
3438
String vmId;
3539

36-
public DrsPlanResponse(String hostId, String vmId) {
37-
this.hostId = hostId;
40+
public DrsPlanResponse(String vmId, String srcHostId, String destHostId) {
3841
this.vmId = vmId;
42+
this.srcHostId = srcHostId;
43+
this.destHostId = destHostId;
3944
}
4045
}

api/src/main/java/org/apache/cloudstack/cluster/ClusterDrsAlgorithm.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,16 @@
2020
package org.apache.cloudstack.cluster;
2121

2222
import com.cloud.host.Host;
23+
import com.cloud.offering.ServiceOffering;
24+
import com.cloud.utils.Pair;
2325
import com.cloud.utils.Ternary;
2426
import com.cloud.utils.component.Adapter;
2527
import com.cloud.vm.VirtualMachine;
2628
import org.apache.commons.math3.stat.descriptive.moment.Mean;
2729
import org.apache.commons.math3.stat.descriptive.moment.StandardDeviation;
2830

2931
import javax.naming.ConfigurationException;
32+
import java.util.ArrayList;
3033
import java.util.List;
3134
import java.util.Map;
3235

@@ -56,7 +59,7 @@ public interface ClusterDrsAlgorithm extends Adapter {
5659
* @param requiresStorageMotion true if storage motion is required
5760
* @return Ternary object containing improvement, cost, benefit
5861
*/
59-
Ternary<Double, Double, Double> getMetrics(long clusterId, VirtualMachine vm, Host destHost, Map<Long, Long> hostCpuUsedMap, Map<Long, Long> hostMemoryUsedMap, Boolean requiresStorageMotion);
62+
Ternary<Double, Double, Double> getMetrics(long clusterId, VirtualMachine vm, ServiceOffering serviceOffering, Host destHost, Map<Long, Long> hostCpuUsedMap, Map<Long, Long> hostMemoryUsedMap, Boolean requiresStorageMotion);
6063

6164
/**
6265
* Mean is the average of a collection or set of metrics. In context of a DRS
@@ -103,4 +106,28 @@ default Double getClusterImbalance(List<Long> metricList) {
103106
Double clusterStandardDeviation = getClusterStandardDeviation(metricList, clusterMeanMetric);
104107
return clusterStandardDeviation / clusterMeanMetric;
105108
}
109+
110+
111+
default Pair<Double, Double> getImbalancePostMigration(ServiceOffering serviceOffering, VirtualMachine vm, Host destHost, Map<Long, Long> hostCpuUsedMap, Map<Long, Long> hostMemoryUsedMap) {
112+
List<Long> postCpuList = new ArrayList<>();
113+
List<Long> postMemoryList = new ArrayList<>();
114+
final int vmCpu = serviceOffering.getCpu() * serviceOffering.getSpeed();
115+
final long vmRam = serviceOffering.getRamSize() * 1024L * 1024L;
116+
117+
for (Long hostId : hostCpuUsedMap.keySet()) {
118+
long cpu = hostCpuUsedMap.get(hostId);
119+
long memory = hostMemoryUsedMap.get(hostId);
120+
if (hostId == destHost.getId()) {
121+
postCpuList.add(cpu + vmCpu);
122+
postMemoryList.add(memory + vmRam);
123+
} else if (hostId.equals(vm.getHostId())) {
124+
postCpuList.add(cpu - vmCpu);
125+
postMemoryList.add(memory - vmRam);
126+
} else {
127+
postCpuList.add(cpu);
128+
postMemoryList.add(memory);
129+
}
130+
}
131+
return new Pair<>(getClusterImbalance(postCpuList), getClusterImbalance(postMemoryList));
132+
}
106133
}

api/src/main/java/org/apache/cloudstack/cluster/ClusterDrsService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
package org.apache.cloudstack.cluster;
2121

2222
import com.cloud.host.Host;
23-
import com.cloud.utils.Pair;
23+
import com.cloud.utils.Ternary;
2424
import com.cloud.utils.component.Manager;
2525
import com.cloud.utils.concurrency.Scheduler;
2626
import com.cloud.vm.VirtualMachine;
@@ -53,5 +53,5 @@ public interface ClusterDrsService extends Manager, Configurable, Scheduler {
5353
* @param cmd the GenerateClusterDrsPlanCmd object containing the command parameters
5454
* @return a SuccessResponse object indicating the success of the operation
5555
*/
56-
List<Pair<Host, VirtualMachine>> generateDrsPlan(GenerateClusterDrsPlanCmd cmd);
56+
List<Ternary<VirtualMachine, Host, Host>> generateDrsPlan(GenerateClusterDrsPlanCmd cmd);
5757
}

plugins/drs/cluster/balanced/src/main/java/org/apache/cloudstack/cluster/Balanced.java

Lines changed: 8 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,13 @@
1919

2020
package org.apache.cloudstack.cluster;
2121

22-
import com.cloud.api.query.dao.HostJoinDao;
2322
import com.cloud.host.Host;
2423
import com.cloud.offering.ServiceOffering;
25-
import com.cloud.service.dao.ServiceOfferingDao;
24+
import com.cloud.utils.Pair;
2625
import com.cloud.utils.Ternary;
2726
import com.cloud.utils.component.AdapterBase;
2827
import com.cloud.vm.VirtualMachine;
2928

30-
import javax.inject.Inject;
3129
import javax.naming.ConfigurationException;
3230
import java.util.ArrayList;
3331
import java.util.List;
@@ -38,12 +36,6 @@
3836

3937
public class Balanced extends AdapterBase implements ClusterDrsAlgorithm {
4038

41-
@Inject
42-
ServiceOfferingDao serviceOfferingDao;
43-
44-
@Inject
45-
HostJoinDao hostJoinDao;
46-
4739
@Override
4840
public String getName() {
4941
return "balanced";
@@ -70,42 +62,16 @@ public boolean needsDrs(long clusterId, List<Long> cpuList, List<Long> memoryLis
7062
}
7163

7264
@Override
73-
public Ternary<Double, Double, Double> getMetrics(long clusterId, VirtualMachine vm, Host destHost, Map<Long, Long> hostCpuUsedMap, Map<Long, Long> hostMemoryUsedMap, Boolean requiresStorageMotion) {
74-
List<Long> cpuList = new ArrayList<>();
75-
List<Long> memoryList = new ArrayList<>();
76-
List<Long> postCpuList = new ArrayList<>();
77-
List<Long> postMemoryList = new ArrayList<>();
78-
ServiceOffering serviceOffering = serviceOfferingDao.findByIdIncludingRemoved(vm.getId(), vm.getServiceOfferingId());
79-
80-
for (Long hostId : hostCpuUsedMap.keySet()) {
81-
long cpu = hostCpuUsedMap.get(hostId);
82-
long memory = hostMemoryUsedMap.get(hostId);
83-
if (hostId == destHost.getId()) {
84-
if (memory + serviceOffering.getRamSize() > destHost.getTotalMemory()) {
85-
return new Ternary<>(-1.0, 1.0, -1.0);
86-
}
87-
// TODO: Revisit this check for overcommitting of resources
88-
if (cpu + serviceOffering.getCpu() > destHost.getCpus()) {
89-
return new Ternary<>(-1.0, 1.0, -1.0);
90-
}
91-
postCpuList.add(cpu + serviceOffering.getCpu());
92-
postMemoryList.add(memory + serviceOffering.getRamSize());
93-
} else if (hostId.equals(vm.getHostId())) {
94-
postCpuList.add(cpu - serviceOffering.getCpu());
95-
postMemoryList.add(memory - serviceOffering.getRamSize());
96-
} else {
97-
postCpuList.add(cpu);
98-
postMemoryList.add(memory);
99-
}
100-
}
65+
public Ternary<Double, Double, Double> getMetrics(long clusterId, VirtualMachine vm, ServiceOffering serviceOffering, Host destHost, Map<Long, Long> hostCpuUsedMap, Map<Long, Long> hostMemoryUsedMap, Boolean requiresStorageMotion) {
66+
Double preCpuImbalance = getClusterImbalance(new ArrayList<>(hostCpuUsedMap.values()));
67+
Double preMemoryImbalance = getClusterImbalance(new ArrayList<>(hostMemoryUsedMap.values()));
10168

102-
Double preCpuImbalance = getClusterImbalance(cpuList);
103-
Double preMemoryImbalance = getClusterImbalance(memoryList);
104-
Double postCpuImbalance = getClusterImbalance(postCpuList);
105-
Double postMemoryImbalance = getClusterImbalance(postMemoryList);
69+
Pair<Double, Double> imbalancePair = getImbalancePostMigration(serviceOffering, vm, destHost, hostCpuUsedMap, hostMemoryUsedMap);
70+
Double postCpuImbalance = imbalancePair.first();
71+
Double postMemoryImbalance = imbalancePair.second();
10672

10773
double cost = serviceOffering.getRamSize();
108-
double benefit = (preMemoryImbalance - postMemoryImbalance) * destHost.getTotalMemory();
74+
double benefit = (preMemoryImbalance - postMemoryImbalance) * destHost.getTotalMemory() / (1024L * 1024L);
10975

11076
String metric = ClusterDrsMetric.valueIn(clusterId);
11177
final double improvement;

0 commit comments

Comments
 (0)