mirror of
https://github.com/apache/cloudstack.git
synced 2025-10-26 08:42:29 +01:00
DRS: Use free metrics insteado of used for computation (#8458)
This PR makes changes to use cluster's free metrics instead of used while computing imbalance for the cluster. This allows DRS to run for clusters where hosts doesn't have the same amount of metrics.
This commit is contained in:
parent
b8d3e342be
commit
4f40eae1c4
@ -65,18 +65,18 @@ public interface ClusterDrsAlgorithm extends Adapter {
|
|||||||
* the service offering for the virtual machine
|
* the service offering for the virtual machine
|
||||||
* @param destHost
|
* @param destHost
|
||||||
* the destination host for the virtual machine
|
* the destination host for the virtual machine
|
||||||
* @param hostCpuUsedMap
|
* @param hostCpuFreeMap
|
||||||
* a map of host IDs to the amount of CPU used on each host
|
* a map of host IDs to the amount of CPU free on each host
|
||||||
* @param hostMemoryUsedMap
|
* @param hostMemoryFreeMap
|
||||||
* a map of host IDs to the amount of memory used on each host
|
* a map of host IDs to the amount of memory free on each host
|
||||||
* @param requiresStorageMotion
|
* @param requiresStorageMotion
|
||||||
* whether storage motion is required for the virtual machine
|
* whether storage motion is required for the virtual machine
|
||||||
*
|
*
|
||||||
* @return a ternary containing improvement, cost, benefit
|
* @return a ternary containing improvement, cost, benefit
|
||||||
*/
|
*/
|
||||||
Ternary<Double, Double, Double> getMetrics(long clusterId, VirtualMachine vm, ServiceOffering serviceOffering,
|
Ternary<Double, Double, Double> getMetrics(long clusterId, VirtualMachine vm, ServiceOffering serviceOffering,
|
||||||
Host destHost, Map<Long, Long> hostCpuUsedMap,
|
Host destHost, Map<Long, Long> hostCpuFreeMap,
|
||||||
Map<Long, Long> hostMemoryUsedMap, Boolean requiresStorageMotion);
|
Map<Long, Long> hostMemoryFreeMap, Boolean requiresStorageMotion);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Calculates the imbalance of the cluster after a virtual machine migration.
|
* Calculates the imbalance of the cluster after a virtual machine migration.
|
||||||
@ -87,30 +87,30 @@ public interface ClusterDrsAlgorithm extends Adapter {
|
|||||||
* the virtual machine being migrated
|
* the virtual machine being migrated
|
||||||
* @param destHost
|
* @param destHost
|
||||||
* the destination host for the virtual machine
|
* the destination host for the virtual machine
|
||||||
* @param hostCpuUsedMap
|
* @param hostCpuFreeMap
|
||||||
* a map of host IDs to the amount of CPU used on each host
|
* a map of host IDs to the amount of CPU free on each host
|
||||||
* @param hostMemoryUsedMap
|
* @param hostMemoryFreeMap
|
||||||
* a map of host IDs to the amount of memory used on each host
|
* a map of host IDs to the amount of memory free on each host
|
||||||
*
|
*
|
||||||
* @return a pair containing the CPU and memory imbalance of the cluster after the migration
|
* @return a pair containing the CPU and memory imbalance of the cluster after the migration
|
||||||
*/
|
*/
|
||||||
default Pair<Double, Double> getImbalancePostMigration(ServiceOffering serviceOffering, VirtualMachine vm,
|
default Pair<Double, Double> getImbalancePostMigration(ServiceOffering serviceOffering, VirtualMachine vm,
|
||||||
Host destHost, Map<Long, Long> hostCpuUsedMap,
|
Host destHost, Map<Long, Long> hostCpuFreeMap,
|
||||||
Map<Long, Long> hostMemoryUsedMap) {
|
Map<Long, Long> hostMemoryFreeMap) {
|
||||||
List<Long> postCpuList = new ArrayList<>();
|
List<Long> postCpuList = new ArrayList<>();
|
||||||
List<Long> postMemoryList = new ArrayList<>();
|
List<Long> postMemoryList = new ArrayList<>();
|
||||||
final int vmCpu = serviceOffering.getCpu() * serviceOffering.getSpeed();
|
final int vmCpu = serviceOffering.getCpu() * serviceOffering.getSpeed();
|
||||||
final long vmRam = serviceOffering.getRamSize() * 1024L * 1024L;
|
final long vmRam = serviceOffering.getRamSize() * 1024L * 1024L;
|
||||||
|
|
||||||
for (Long hostId : hostCpuUsedMap.keySet()) {
|
for (Long hostId : hostCpuFreeMap.keySet()) {
|
||||||
long cpu = hostCpuUsedMap.get(hostId);
|
long cpu = hostCpuFreeMap.get(hostId);
|
||||||
long memory = hostMemoryUsedMap.get(hostId);
|
long memory = hostMemoryFreeMap.get(hostId);
|
||||||
if (hostId == destHost.getId()) {
|
if (hostId == destHost.getId()) {
|
||||||
postCpuList.add(cpu + vmCpu);
|
|
||||||
postMemoryList.add(memory + vmRam);
|
|
||||||
} else if (hostId.equals(vm.getHostId())) {
|
|
||||||
postCpuList.add(cpu - vmCpu);
|
postCpuList.add(cpu - vmCpu);
|
||||||
postMemoryList.add(memory - vmRam);
|
postMemoryList.add(memory - vmRam);
|
||||||
|
} else if (hostId.equals(vm.getHostId())) {
|
||||||
|
postCpuList.add(cpu + vmCpu);
|
||||||
|
postMemoryList.add(memory + vmRam);
|
||||||
} else {
|
} else {
|
||||||
postCpuList.add(cpu);
|
postCpuList.add(cpu);
|
||||||
postMemoryList.add(memory);
|
postMemoryList.add(memory);
|
||||||
|
|||||||
@ -68,7 +68,7 @@ public class BalancedTest {
|
|||||||
|
|
||||||
List<Long> cpuList, memoryList;
|
List<Long> cpuList, memoryList;
|
||||||
|
|
||||||
Map<Long, Long> hostCpuUsedMap, hostMemoryUsedMap;
|
Map<Long, Long> hostCpuFreeMap, hostMemoryFreeMap;
|
||||||
|
|
||||||
|
|
||||||
@Mock
|
@Mock
|
||||||
@ -105,13 +105,13 @@ public class BalancedTest {
|
|||||||
cpuList = Arrays.asList(1L, 2L);
|
cpuList = Arrays.asList(1L, 2L);
|
||||||
memoryList = Arrays.asList(512L, 2048L);
|
memoryList = Arrays.asList(512L, 2048L);
|
||||||
|
|
||||||
hostCpuUsedMap = new HashMap<>();
|
hostCpuFreeMap = new HashMap<>();
|
||||||
hostCpuUsedMap.put(1L, 1000L);
|
hostCpuFreeMap.put(1L, 2000L);
|
||||||
hostCpuUsedMap.put(2L, 2000L);
|
hostCpuFreeMap.put(2L, 1000L);
|
||||||
|
|
||||||
hostMemoryUsedMap = new HashMap<>();
|
hostMemoryFreeMap = new HashMap<>();
|
||||||
hostMemoryUsedMap.put(1L, 512L * 1024L * 1024L);
|
hostMemoryFreeMap.put(1L, 2048L * 1024L * 1024L);
|
||||||
hostMemoryUsedMap.put(2L, 2048L * 1024L * 1024L);
|
hostMemoryFreeMap.put(2L, 512L * 1024L * 1024L);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void overrideDefaultConfigValue(final ConfigKey configKey, final String name,
|
private void overrideDefaultConfigValue(final ConfigKey configKey, final String name,
|
||||||
@ -191,7 +191,7 @@ public class BalancedTest {
|
|||||||
public void getMetricsWithCpu() throws NoSuchFieldException, IllegalAccessException {
|
public void getMetricsWithCpu() throws NoSuchFieldException, IllegalAccessException {
|
||||||
overrideDefaultConfigValue(ClusterDrsMetric, "_defaultValue", "cpu");
|
overrideDefaultConfigValue(ClusterDrsMetric, "_defaultValue", "cpu");
|
||||||
Ternary<Double, Double, Double> result = balanced.getMetrics(clusterId, vm3, serviceOffering, destHost,
|
Ternary<Double, Double, Double> result = balanced.getMetrics(clusterId, vm3, serviceOffering, destHost,
|
||||||
hostCpuUsedMap, hostMemoryUsedMap, false);
|
hostCpuFreeMap, hostMemoryFreeMap, false);
|
||||||
assertEquals(0.0, result.first(), 0.01);
|
assertEquals(0.0, result.first(), 0.01);
|
||||||
assertEquals(0.0, result.second(), 0.0);
|
assertEquals(0.0, result.second(), 0.0);
|
||||||
assertEquals(1.0, result.third(), 0.0);
|
assertEquals(1.0, result.third(), 0.0);
|
||||||
@ -205,7 +205,7 @@ public class BalancedTest {
|
|||||||
public void getMetricsWithMemory() throws NoSuchFieldException, IllegalAccessException {
|
public void getMetricsWithMemory() throws NoSuchFieldException, IllegalAccessException {
|
||||||
overrideDefaultConfigValue(ClusterDrsMetric, "_defaultValue", "memory");
|
overrideDefaultConfigValue(ClusterDrsMetric, "_defaultValue", "memory");
|
||||||
Ternary<Double, Double, Double> result = balanced.getMetrics(clusterId, vm3, serviceOffering, destHost,
|
Ternary<Double, Double, Double> result = balanced.getMetrics(clusterId, vm3, serviceOffering, destHost,
|
||||||
hostCpuUsedMap, hostMemoryUsedMap, false);
|
hostCpuFreeMap, hostMemoryFreeMap, false);
|
||||||
assertEquals(0.4, result.first(), 0.01);
|
assertEquals(0.4, result.first(), 0.01);
|
||||||
assertEquals(0, result.second(), 0.0);
|
assertEquals(0, result.second(), 0.0);
|
||||||
assertEquals(1, result.third(), 0.0);
|
assertEquals(1, result.third(), 0.0);
|
||||||
@ -219,7 +219,7 @@ public class BalancedTest {
|
|||||||
public void getMetricsWithDefault() throws NoSuchFieldException, IllegalAccessException {
|
public void getMetricsWithDefault() throws NoSuchFieldException, IllegalAccessException {
|
||||||
overrideDefaultConfigValue(ClusterDrsMetric, "_defaultValue", "both");
|
overrideDefaultConfigValue(ClusterDrsMetric, "_defaultValue", "both");
|
||||||
Ternary<Double, Double, Double> result = balanced.getMetrics(clusterId, vm3, serviceOffering, destHost,
|
Ternary<Double, Double, Double> result = balanced.getMetrics(clusterId, vm3, serviceOffering, destHost,
|
||||||
hostCpuUsedMap, hostMemoryUsedMap, false);
|
hostCpuFreeMap, hostMemoryFreeMap, false);
|
||||||
assertEquals(0.4, result.first(), 0.01);
|
assertEquals(0.4, result.first(), 0.01);
|
||||||
assertEquals(0, result.second(), 0.0);
|
assertEquals(0, result.second(), 0.0);
|
||||||
assertEquals(1, result.third(), 0.0);
|
assertEquals(1, result.third(), 0.0);
|
||||||
|
|||||||
@ -66,7 +66,7 @@ public class CondensedTest {
|
|||||||
|
|
||||||
List<Long> cpuList, memoryList;
|
List<Long> cpuList, memoryList;
|
||||||
|
|
||||||
Map<Long, Long> hostCpuUsedMap, hostMemoryUsedMap;
|
Map<Long, Long> hostCpuFreeMap, hostMemoryFreeMap;
|
||||||
|
|
||||||
|
|
||||||
private AutoCloseable closeable;
|
private AutoCloseable closeable;
|
||||||
@ -98,13 +98,13 @@ public class CondensedTest {
|
|||||||
cpuList = Arrays.asList(1L, 2L);
|
cpuList = Arrays.asList(1L, 2L);
|
||||||
memoryList = Arrays.asList(512L, 2048L);
|
memoryList = Arrays.asList(512L, 2048L);
|
||||||
|
|
||||||
hostCpuUsedMap = new HashMap<>();
|
hostCpuFreeMap = new HashMap<>();
|
||||||
hostCpuUsedMap.put(1L, 1000L);
|
hostCpuFreeMap.put(1L, 2000L);
|
||||||
hostCpuUsedMap.put(2L, 2000L);
|
hostCpuFreeMap.put(2L, 1000L);
|
||||||
|
|
||||||
hostMemoryUsedMap = new HashMap<>();
|
hostMemoryFreeMap = new HashMap<>();
|
||||||
hostMemoryUsedMap.put(1L, 512L * 1024L * 1024L);
|
hostMemoryFreeMap.put(1L, 2048L * 1024L * 1024L);
|
||||||
hostMemoryUsedMap.put(2L, 2048L * 1024L * 1024L);
|
hostMemoryFreeMap.put(2L, 512L * 1024L * 1024L);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void overrideDefaultConfigValue(final ConfigKey configKey,
|
private void overrideDefaultConfigValue(final ConfigKey configKey,
|
||||||
@ -185,7 +185,7 @@ public class CondensedTest {
|
|||||||
public void getMetricsWithCpu() throws NoSuchFieldException, IllegalAccessException {
|
public void getMetricsWithCpu() throws NoSuchFieldException, IllegalAccessException {
|
||||||
overrideDefaultConfigValue(ClusterDrsMetric, "_defaultValue", "cpu");
|
overrideDefaultConfigValue(ClusterDrsMetric, "_defaultValue", "cpu");
|
||||||
Ternary<Double, Double, Double> result = condensed.getMetrics(clusterId, vm3, serviceOffering, destHost,
|
Ternary<Double, Double, Double> result = condensed.getMetrics(clusterId, vm3, serviceOffering, destHost,
|
||||||
hostCpuUsedMap, hostMemoryUsedMap, false);
|
hostCpuFreeMap, hostMemoryFreeMap, false);
|
||||||
assertEquals(0.0, result.first(), 0.0);
|
assertEquals(0.0, result.first(), 0.0);
|
||||||
assertEquals(0, result.second(), 0.0);
|
assertEquals(0, result.second(), 0.0);
|
||||||
assertEquals(1, result.third(), 0.0);
|
assertEquals(1, result.third(), 0.0);
|
||||||
@ -199,7 +199,7 @@ public class CondensedTest {
|
|||||||
public void getMetricsWithMemory() throws NoSuchFieldException, IllegalAccessException {
|
public void getMetricsWithMemory() throws NoSuchFieldException, IllegalAccessException {
|
||||||
overrideDefaultConfigValue(ClusterDrsMetric, "_defaultValue", "memory");
|
overrideDefaultConfigValue(ClusterDrsMetric, "_defaultValue", "memory");
|
||||||
Ternary<Double, Double, Double> result = condensed.getMetrics(clusterId, vm3, serviceOffering, destHost,
|
Ternary<Double, Double, Double> result = condensed.getMetrics(clusterId, vm3, serviceOffering, destHost,
|
||||||
hostCpuUsedMap, hostMemoryUsedMap, false);
|
hostCpuFreeMap, hostMemoryFreeMap, false);
|
||||||
assertEquals(-0.4, result.first(), 0.01);
|
assertEquals(-0.4, result.first(), 0.01);
|
||||||
assertEquals(0, result.second(), 0.0);
|
assertEquals(0, result.second(), 0.0);
|
||||||
assertEquals(1, result.third(), 0.0);
|
assertEquals(1, result.third(), 0.0);
|
||||||
@ -213,7 +213,7 @@ public class CondensedTest {
|
|||||||
public void getMetricsWithDefault() throws NoSuchFieldException, IllegalAccessException {
|
public void getMetricsWithDefault() throws NoSuchFieldException, IllegalAccessException {
|
||||||
overrideDefaultConfigValue(ClusterDrsMetric, "_defaultValue", "both");
|
overrideDefaultConfigValue(ClusterDrsMetric, "_defaultValue", "both");
|
||||||
Ternary<Double, Double, Double> result = condensed.getMetrics(clusterId, vm3, serviceOffering, destHost,
|
Ternary<Double, Double, Double> result = condensed.getMetrics(clusterId, vm3, serviceOffering, destHost,
|
||||||
hostCpuUsedMap, hostMemoryUsedMap, false);
|
hostCpuFreeMap, hostMemoryFreeMap, false);
|
||||||
assertEquals(-0.4, result.first(), 0.0001);
|
assertEquals(-0.4, result.first(), 0.0001);
|
||||||
assertEquals(0, result.second(), 0.0);
|
assertEquals(0, result.second(), 0.0);
|
||||||
assertEquals(1, result.third(), 0.0);
|
assertEquals(1, result.third(), 0.0);
|
||||||
|
|||||||
@ -354,9 +354,9 @@ public class ClusterDrsServiceImpl extends ManagerBase implements ClusterDrsServ
|
|||||||
hostList.stream().map(HostVO::getId).toArray(Long[]::new));
|
hostList.stream().map(HostVO::getId).toArray(Long[]::new));
|
||||||
|
|
||||||
Map<Long, Long> hostCpuMap = hostJoinList.stream().collect(Collectors.toMap(HostJoinVO::getId,
|
Map<Long, Long> hostCpuMap = hostJoinList.stream().collect(Collectors.toMap(HostJoinVO::getId,
|
||||||
hostJoin -> hostJoin.getCpuUsedCapacity() + hostJoin.getCpuReservedCapacity()));
|
hostJoin -> hostJoin.getCpus() * hostJoin.getSpeed() - hostJoin.getCpuReservedCapacity() - hostJoin.getCpuUsedCapacity()));
|
||||||
Map<Long, Long> hostMemoryMap = hostJoinList.stream().collect(Collectors.toMap(HostJoinVO::getId,
|
Map<Long, Long> hostMemoryMap = hostJoinList.stream().collect(Collectors.toMap(HostJoinVO::getId,
|
||||||
hostJoin -> hostJoin.getMemUsedCapacity() + hostJoin.getMemReservedCapacity()));
|
hostJoin -> hostJoin.getTotalMemory() - hostJoin.getMemUsedCapacity() - hostJoin.getMemReservedCapacity()));
|
||||||
|
|
||||||
Map<Long, ServiceOffering> vmIdServiceOfferingMap = new HashMap<>();
|
Map<Long, ServiceOffering> vmIdServiceOfferingMap = new HashMap<>();
|
||||||
|
|
||||||
@ -387,10 +387,10 @@ public class ClusterDrsServiceImpl extends ManagerBase implements ClusterDrsServ
|
|||||||
long vmCpu = (long) serviceOffering.getCpu() * serviceOffering.getSpeed();
|
long vmCpu = (long) serviceOffering.getCpu() * serviceOffering.getSpeed();
|
||||||
long vmMemory = serviceOffering.getRamSize() * 1024L * 1024L;
|
long vmMemory = serviceOffering.getRamSize() * 1024L * 1024L;
|
||||||
|
|
||||||
hostCpuMap.put(vm.getHostId(), hostCpuMap.get(vm.getHostId()) - vmCpu);
|
hostCpuMap.put(vm.getHostId(), hostCpuMap.get(vm.getHostId()) + vmCpu);
|
||||||
hostCpuMap.put(destHost.getId(), hostCpuMap.get(destHost.getId()) + vmCpu);
|
hostCpuMap.put(destHost.getId(), hostCpuMap.get(destHost.getId()) - vmCpu);
|
||||||
hostMemoryMap.put(vm.getHostId(), hostMemoryMap.get(vm.getHostId()) - vmMemory);
|
hostMemoryMap.put(vm.getHostId(), hostMemoryMap.get(vm.getHostId()) + vmMemory);
|
||||||
hostMemoryMap.put(destHost.getId(), hostMemoryMap.get(destHost.getId()) + vmMemory);
|
hostMemoryMap.put(destHost.getId(), hostMemoryMap.get(destHost.getId()) - vmMemory);
|
||||||
vm.setHostId(destHost.getId());
|
vm.setHostId(destHost.getId());
|
||||||
iteration++;
|
iteration++;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user