From 9e19ab37d7f53949474bbdaa30404dcb59b961ce Mon Sep 17 00:00:00 2001 From: Gabriel Date: Tue, 6 Aug 2024 09:10:26 -0300 Subject: [PATCH 1/3] allow deploy of VRs on dedicated resources --- .../com/cloud/capacity/dao/CapacityDao.java | 7 ++++- .../cloud/capacity/dao/CapacityDaoImpl.java | 29 +++++++++++++++++-- .../capacity/dao/CapacityDaoImplTest.java | 2 +- .../implicitplanner/ImplicitPlannerTest.java | 2 +- .../com/cloud/deploy/FirstFitPlanner.java | 11 +++---- .../com/cloud/vm/FirstFitPlannerTest.java | 2 +- 6 files changed, 41 insertions(+), 12 deletions(-) diff --git a/engine/schema/src/main/java/com/cloud/capacity/dao/CapacityDao.java b/engine/schema/src/main/java/com/cloud/capacity/dao/CapacityDao.java index 42313efa512f..4cb8b506a0d7 100644 --- a/engine/schema/src/main/java/com/cloud/capacity/dao/CapacityDao.java +++ b/engine/schema/src/main/java/com/cloud/capacity/dao/CapacityDao.java @@ -24,8 +24,13 @@ import com.cloud.utils.Pair; import com.cloud.utils.Ternary; import com.cloud.utils.db.GenericDao; +import org.apache.cloudstack.framework.config.ConfigKey; public interface CapacityDao extends GenericDao { + + ConfigKey allowRoutersOnDedicatedResources = new ConfigKey<>("Advanced", Boolean.class, "allow.routers.on.dedicated.resources", "false", + "Allow deploying virtual routers on dedicated Hosts, Clusters, Pods, and Zones", true); + CapacityVO findByHostIdType(Long hostId, short capacityType); List listByHostIdTypes(Long hostId, List capacityTypes); @@ -40,7 +45,7 @@ public interface CapacityDao extends GenericDao { List findNonSharedStorageForClusterPodZone(Long zoneId, Long podId, Long clusterId); - Pair, Map> orderClustersByAggregateCapacity(long id, long vmId, short capacityType, boolean isZone); + Pair, Map> orderClustersByAggregateCapacity(long id, long vmId, short capacityType, boolean isVr, boolean isZone); Ternary findCapacityByZoneAndHostTag(Long zoneId, String hostTag); diff --git a/engine/schema/src/main/java/com/cloud/capacity/dao/CapacityDaoImpl.java b/engine/schema/src/main/java/com/cloud/capacity/dao/CapacityDaoImpl.java index f65c3cf188cd..386623df5b7e 100644 --- a/engine/schema/src/main/java/com/cloud/capacity/dao/CapacityDaoImpl.java +++ b/engine/schema/src/main/java/com/cloud/capacity/dao/CapacityDaoImpl.java @@ -26,6 +26,8 @@ import javax.inject.Inject; +import org.apache.cloudstack.framework.config.ConfigKey; +import org.apache.cloudstack.framework.config.Configurable; import org.apache.cloudstack.storage.datastore.db.PrimaryDataStoreDao; import org.apache.cloudstack.storage.datastore.db.StoragePoolVO; import org.apache.commons.collections.CollectionUtils; @@ -49,7 +51,7 @@ import com.cloud.utils.exception.CloudRuntimeException; @Component -public class CapacityDaoImpl extends GenericDaoBase implements CapacityDao { +public class CapacityDaoImpl extends GenericDaoBase implements CapacityDao, Configurable { private static final String ADD_ALLOCATED_SQL = "UPDATE `cloud`.`op_host_capacity` SET used_capacity = used_capacity + ? WHERE host_id = ? AND capacity_type = ?"; private static final String SUBTRACT_ALLOCATED_SQL = @@ -87,6 +89,13 @@ public class CapacityDaoImpl extends GenericDaoBase implements "LEFT JOIN dedicated_resources dr_cluster ON dr_cluster.cluster_id IS NOT NULL AND dr_cluster.cluster_id = host.cluster_id " + "LEFT JOIN dedicated_resources dr_host ON dr_host.host_id IS NOT NULL AND dr_host.host_id = host.id "; + private static final String ORDER_CLUSTERS_BY_AGGREGATE_CAPACITY_INCLUDE_DEDICATED_TO_DOMAIN_JOIN_1 = + "JOIN host ON capacity.host_id = host.id " + + "LEFT JOIN (SELECT affinity_group.id, agvm.instance_id FROM affinity_group_vm_map agvm JOIN affinity_group ON agvm.affinity_group_id = affinity_group.id AND affinity_group.type='ExplicitDedication') AS ag ON ag.instance_id = ? " + + "LEFT JOIN dedicated_resources dr_pod ON dr_pod.pod_id IS NOT NULL AND dr_pod.pod_id = host.pod_id AND dr_pod.account_id IS NOT NULL " + + "LEFT JOIN dedicated_resources dr_cluster ON dr_cluster.cluster_id IS NOT NULL AND dr_cluster.cluster_id = host.cluster_id AND dr_cluster.account_id IS NOT NULL " + + "LEFT JOIN dedicated_resources dr_host ON dr_host.host_id IS NOT NULL AND dr_host.host_id = host.id AND dr_host.account_id IS NOT NULL "; + private static final String ORDER_CLUSTERS_BY_AGGREGATE_CAPACITY_JOIN_2 = " AND ((ag.id IS NULL AND dr_pod.pod_id IS NULL AND dr_cluster.cluster_id IS NULL AND dr_host.host_id IS NULL) OR " + "(dr_pod.affinity_group_id = ag.id OR dr_cluster.affinity_group_id = ag.id OR dr_host.affinity_group_id = ag.id))"; @@ -986,7 +995,7 @@ public boolean removeBy(Short capacityType, Long zoneId, Long podId, Long cluste } @Override - public Pair, Map> orderClustersByAggregateCapacity(long id, long vmId, short capacityTypeForOrdering, boolean isZone) { + public Pair, Map> orderClustersByAggregateCapacity(long id, long vmId, short capacityTypeForOrdering, boolean isVr, boolean isZone) { TransactionLegacy txn = TransactionLegacy.currentTxn(); PreparedStatement pstmt = null; List result = new ArrayList(); @@ -998,7 +1007,12 @@ public Pair, Map> orderClustersByAggregateCapacity(long sql.append(ORDER_CLUSTERS_BY_AGGREGATE_OVERCOMMIT_CAPACITY_PART1); } - sql.append(ORDER_CLUSTERS_BY_AGGREGATE_CAPACITY_JOIN_1); + if (isVr && allowRoutersOnDedicatedResources.value()) { + sql.append(ORDER_CLUSTERS_BY_AGGREGATE_CAPACITY_INCLUDE_DEDICATED_TO_DOMAIN_JOIN_1); + } else { + sql.append(ORDER_CLUSTERS_BY_AGGREGATE_CAPACITY_JOIN_1); + } + if (isZone) { sql.append("WHERE capacity.capacity_state = 'Enabled' AND capacity.data_center_id = ?"); } else { @@ -1291,4 +1305,13 @@ public float findClusterConsumption(Long clusterId, short capacityType, long com return 0; } + @Override + public ConfigKey[] getConfigKeys() { + return new ConfigKey[] {allowRoutersOnDedicatedResources}; + } + + @Override + public String getConfigComponentName() { + return CapacityDaoImpl.class.getSimpleName(); + } } diff --git a/engine/schema/src/test/java/com/cloud/capacity/dao/CapacityDaoImplTest.java b/engine/schema/src/test/java/com/cloud/capacity/dao/CapacityDaoImplTest.java index f9528d5d57ff..57f48493c1e2 100644 --- a/engine/schema/src/test/java/com/cloud/capacity/dao/CapacityDaoImplTest.java +++ b/engine/schema/src/test/java/com/cloud/capacity/dao/CapacityDaoImplTest.java @@ -250,7 +250,7 @@ public void testOrderClustersByAggregateCapacityEmptyResult() throws Exception { when(pstmt.executeQuery()).thenReturn(resultSet); when(resultSet.next()).thenReturn(false); - Pair, Map> result = capacityDao.orderClustersByAggregateCapacity(1L, 1L, (short) 1, true); + Pair, Map> result = capacityDao.orderClustersByAggregateCapacity(1L, 1L, (short) 1, false,true); assertNotNull(result); assertTrue(result.first().isEmpty()); assertTrue(result.second().isEmpty()); diff --git a/plugins/deployment-planners/implicit-dedication/src/test/java/org/apache/cloudstack/implicitplanner/ImplicitPlannerTest.java b/plugins/deployment-planners/implicit-dedication/src/test/java/org/apache/cloudstack/implicitplanner/ImplicitPlannerTest.java index d859ebd0ffba..73a426add715 100644 --- a/plugins/deployment-planners/implicit-dedication/src/test/java/org/apache/cloudstack/implicitplanner/ImplicitPlannerTest.java +++ b/plugins/deployment-planners/implicit-dedication/src/test/java/org/apache/cloudstack/implicitplanner/ImplicitPlannerTest.java @@ -366,7 +366,7 @@ private void initializeForTest(VirtualMachineProfileImpl vmProfile, DataCenterDe clusterCapacityMap.put(2L, 2048D); clusterCapacityMap.put(3L, 2048D); Pair, Map> clustersOrderedByCapacity = new Pair, Map>(clustersWithEnoughCapacity, clusterCapacityMap); - when(capacityDao.orderClustersByAggregateCapacity(dataCenterId, 12L, Capacity.CAPACITY_TYPE_CPU, true)).thenReturn(clustersOrderedByCapacity); + when(capacityDao.orderClustersByAggregateCapacity(dataCenterId, 12L, Capacity.CAPACITY_TYPE_CPU, false, true)).thenReturn(clustersOrderedByCapacity); List disabledClusters = new ArrayList(); List clustersWithDisabledPods = new ArrayList(); diff --git a/server/src/main/java/com/cloud/deploy/FirstFitPlanner.java b/server/src/main/java/com/cloud/deploy/FirstFitPlanner.java index 3aab852ba7fc..b3593a02a134 100644 --- a/server/src/main/java/com/cloud/deploy/FirstFitPlanner.java +++ b/server/src/main/java/com/cloud/deploy/FirstFitPlanner.java @@ -398,9 +398,10 @@ private List scanClustersForDestinationInZoneOrPod(long id, boolean isZone DataCenter dc = dcDao.findById(vm.getDataCenterId()); int requiredCpu = offering.getCpu() * offering.getSpeed(); long requiredRam = offering.getRamSize() * 1024L * 1024L; + boolean isVr = VirtualMachine.Type.DomainRouter.equals(vmProfile.getType()); //list clusters under this zone by cpu and ram capacity - Pair, Map> clusterCapacityInfo = listClustersByCapacity(id, vmProfile.getId(), requiredCpu, requiredRam, avoid, isZone); + Pair, Map> clusterCapacityInfo = listClustersByCapacity(id, vmProfile.getId(), requiredCpu, requiredRam, isVr, isZone); List prioritizedClusterIds = clusterCapacityInfo.first(); if (!prioritizedClusterIds.isEmpty()) { if (avoid.getClustersToAvoid() != null) { @@ -458,7 +459,7 @@ protected List reorderPods(Pair, Map> podCapacity return podIdsByCapacity; } - protected Pair, Map> listClustersByCapacity(long id, long vmId, int requiredCpu, long requiredRam, ExcludeList avoid, boolean isZone) { + protected Pair, Map> listClustersByCapacity(long id, long vmId, int requiredCpu, long requiredRam, boolean isVr, boolean isZone) { //look at the aggregate available cpu and ram per cluster //although an aggregate value may be false indicator that a cluster can host a vm, it will at the least eliminate those clusters which definitely cannot @@ -474,7 +475,7 @@ protected Pair, Map> listClustersByCapacity(long id, lo } - Pair, Map> result = getOrderedClustersByCapacity(id, vmId, isZone); + Pair, Map> result = getOrderedClustersByCapacity(id, vmId, isVr, isZone); List clusterIdsOrderedByAggregateCapacity = result.first(); //only keep the clusters that have enough capacity to host this VM if (logger.isTraceEnabled()) { @@ -554,14 +555,14 @@ public Map getPodByCombinedCapacities(List capacities, } - private Pair, Map> getOrderedClustersByCapacity(long id, long vmId, boolean isZone) { + private Pair, Map> getOrderedClustersByCapacity(long id, long vmId, boolean isVr, boolean isZone) { double cpuToMemoryWeight = ConfigurationManager.HostCapacityTypeCpuMemoryWeight.value(); short capacityType = getHostCapacityTypeToOrderCluster( configDao.getValue(Config.HostCapacityTypeToOrderClusters.key()), cpuToMemoryWeight); logger.debug("CapacityType: {} is used for Cluster ordering", getCapacityTypeName(capacityType)); if (capacityType >= 0) { // for capacityType other than COMBINED - return capacityDao.orderClustersByAggregateCapacity(id, vmId, capacityType, isZone); + return capacityDao.orderClustersByAggregateCapacity(id, vmId, capacityType, isVr, isZone); } Long zoneId = isZone ? id : null; diff --git a/server/src/test/java/com/cloud/vm/FirstFitPlannerTest.java b/server/src/test/java/com/cloud/vm/FirstFitPlannerTest.java index 5b877cc66169..1742148c8143 100644 --- a/server/src/test/java/com/cloud/vm/FirstFitPlannerTest.java +++ b/server/src/test/java/com/cloud/vm/FirstFitPlannerTest.java @@ -373,7 +373,7 @@ private void initializeForTest(VirtualMachineProfileImpl vmProfile, DataCenterDe clusterCapacityMap.put(6L, 2048D); Pair, Map> clustersOrderedByCapacity = new Pair, Map>(clustersWithEnoughCapacity, clusterCapacityMap); - when(capacityDao.orderClustersByAggregateCapacity(dataCenterId, 12L, Capacity.CAPACITY_TYPE_CPU, true)).thenReturn(clustersOrderedByCapacity); + when(capacityDao.orderClustersByAggregateCapacity(dataCenterId, 12L, Capacity.CAPACITY_TYPE_CPU, false, true)).thenReturn(clustersOrderedByCapacity); List disabledClusters = new ArrayList(); List clustersWithDisabledPods = new ArrayList(); From 42c56253b5b93f2f1787be9ae96ab043f6234b62 Mon Sep 17 00:00:00 2001 From: Nicole Schmidt Date: Wed, 5 Feb 2025 10:03:28 -0300 Subject: [PATCH 2/3] Move configuration to Planner hierarchy --- .../cloud/deploy/DeploymentClusterPlanner.java | 8 ++++++++ .../com/cloud/capacity/dao/CapacityDao.java | 6 +----- .../cloud/capacity/dao/CapacityDaoImpl.java | 18 +++--------------- .../implicitplanner/ImplicitPlannerTest.java | 2 +- .../java/com/cloud/deploy/FirstFitPlanner.java | 4 ++-- .../java/com/cloud/vm/FirstFitPlannerTest.java | 2 +- 6 files changed, 16 insertions(+), 24 deletions(-) diff --git a/api/src/main/java/com/cloud/deploy/DeploymentClusterPlanner.java b/api/src/main/java/com/cloud/deploy/DeploymentClusterPlanner.java index 9471c3d5c84c..3d250b63cd3a 100644 --- a/api/src/main/java/com/cloud/deploy/DeploymentClusterPlanner.java +++ b/api/src/main/java/com/cloud/deploy/DeploymentClusterPlanner.java @@ -68,6 +68,14 @@ public interface DeploymentClusterPlanner extends DeploymentPlanner { ConfigKey.Kind.Select, "random,firstfit,userdispersing,firstfitleastconsumed"); + ConfigKey allowRoutersOnDedicatedResources = new ConfigKey<>( + "Advanced", + Boolean.class, + "allow.routers.on.dedicated.resources", + "false", + "Allow deploying virtual routers on dedicated Hosts, Clusters, Pods, and Zones", + true); + /** * This is called to determine list of possible clusters where a virtual * machine can be deployed. diff --git a/engine/schema/src/main/java/com/cloud/capacity/dao/CapacityDao.java b/engine/schema/src/main/java/com/cloud/capacity/dao/CapacityDao.java index 4cb8b506a0d7..ad24c4741b69 100644 --- a/engine/schema/src/main/java/com/cloud/capacity/dao/CapacityDao.java +++ b/engine/schema/src/main/java/com/cloud/capacity/dao/CapacityDao.java @@ -24,13 +24,9 @@ import com.cloud.utils.Pair; import com.cloud.utils.Ternary; import com.cloud.utils.db.GenericDao; -import org.apache.cloudstack.framework.config.ConfigKey; public interface CapacityDao extends GenericDao { - ConfigKey allowRoutersOnDedicatedResources = new ConfigKey<>("Advanced", Boolean.class, "allow.routers.on.dedicated.resources", "false", - "Allow deploying virtual routers on dedicated Hosts, Clusters, Pods, and Zones", true); - CapacityVO findByHostIdType(Long hostId, short capacityType); List listByHostIdTypes(Long hostId, List capacityTypes); @@ -45,7 +41,7 @@ public interface CapacityDao extends GenericDao { List findNonSharedStorageForClusterPodZone(Long zoneId, Long podId, Long clusterId); - Pair, Map> orderClustersByAggregateCapacity(long id, long vmId, short capacityType, boolean isVr, boolean isZone); + Pair, Map> orderClustersByAggregateCapacity(long id, long vmId, short capacityType, boolean isVr, boolean allowRoutersOnDedicatedResources, boolean isZone); Ternary findCapacityByZoneAndHostTag(Long zoneId, String hostTag); diff --git a/engine/schema/src/main/java/com/cloud/capacity/dao/CapacityDaoImpl.java b/engine/schema/src/main/java/com/cloud/capacity/dao/CapacityDaoImpl.java index 386623df5b7e..044d08e2472d 100644 --- a/engine/schema/src/main/java/com/cloud/capacity/dao/CapacityDaoImpl.java +++ b/engine/schema/src/main/java/com/cloud/capacity/dao/CapacityDaoImpl.java @@ -26,8 +26,6 @@ import javax.inject.Inject; -import org.apache.cloudstack.framework.config.ConfigKey; -import org.apache.cloudstack.framework.config.Configurable; import org.apache.cloudstack.storage.datastore.db.PrimaryDataStoreDao; import org.apache.cloudstack.storage.datastore.db.StoragePoolVO; import org.apache.commons.collections.CollectionUtils; @@ -51,7 +49,7 @@ import com.cloud.utils.exception.CloudRuntimeException; @Component -public class CapacityDaoImpl extends GenericDaoBase implements CapacityDao, Configurable { +public class CapacityDaoImpl extends GenericDaoBase implements CapacityDao { private static final String ADD_ALLOCATED_SQL = "UPDATE `cloud`.`op_host_capacity` SET used_capacity = used_capacity + ? WHERE host_id = ? AND capacity_type = ?"; private static final String SUBTRACT_ALLOCATED_SQL = @@ -995,7 +993,7 @@ public boolean removeBy(Short capacityType, Long zoneId, Long podId, Long cluste } @Override - public Pair, Map> orderClustersByAggregateCapacity(long id, long vmId, short capacityTypeForOrdering, boolean isVr, boolean isZone) { + public Pair, Map> orderClustersByAggregateCapacity(long id, long vmId, short capacityTypeForOrdering, boolean isVr, boolean allowRoutersOnDedicatedResources, boolean isZone) { TransactionLegacy txn = TransactionLegacy.currentTxn(); PreparedStatement pstmt = null; List result = new ArrayList(); @@ -1007,7 +1005,7 @@ public Pair, Map> orderClustersByAggregateCapacity(long sql.append(ORDER_CLUSTERS_BY_AGGREGATE_OVERCOMMIT_CAPACITY_PART1); } - if (isVr && allowRoutersOnDedicatedResources.value()) { + if (isVr && allowRoutersOnDedicatedResources) { sql.append(ORDER_CLUSTERS_BY_AGGREGATE_CAPACITY_INCLUDE_DEDICATED_TO_DOMAIN_JOIN_1); } else { sql.append(ORDER_CLUSTERS_BY_AGGREGATE_CAPACITY_JOIN_1); @@ -1304,14 +1302,4 @@ public float findClusterConsumption(Long clusterId, short capacityType, long com } return 0; } - - @Override - public ConfigKey[] getConfigKeys() { - return new ConfigKey[] {allowRoutersOnDedicatedResources}; - } - - @Override - public String getConfigComponentName() { - return CapacityDaoImpl.class.getSimpleName(); - } } diff --git a/plugins/deployment-planners/implicit-dedication/src/test/java/org/apache/cloudstack/implicitplanner/ImplicitPlannerTest.java b/plugins/deployment-planners/implicit-dedication/src/test/java/org/apache/cloudstack/implicitplanner/ImplicitPlannerTest.java index 73a426add715..2346f67065f2 100644 --- a/plugins/deployment-planners/implicit-dedication/src/test/java/org/apache/cloudstack/implicitplanner/ImplicitPlannerTest.java +++ b/plugins/deployment-planners/implicit-dedication/src/test/java/org/apache/cloudstack/implicitplanner/ImplicitPlannerTest.java @@ -366,7 +366,7 @@ private void initializeForTest(VirtualMachineProfileImpl vmProfile, DataCenterDe clusterCapacityMap.put(2L, 2048D); clusterCapacityMap.put(3L, 2048D); Pair, Map> clustersOrderedByCapacity = new Pair, Map>(clustersWithEnoughCapacity, clusterCapacityMap); - when(capacityDao.orderClustersByAggregateCapacity(dataCenterId, 12L, Capacity.CAPACITY_TYPE_CPU, false, true)).thenReturn(clustersOrderedByCapacity); + when(capacityDao.orderClustersByAggregateCapacity(dataCenterId, 12L, Capacity.CAPACITY_TYPE_CPU, false, false, true)).thenReturn(clustersOrderedByCapacity); List disabledClusters = new ArrayList(); List clustersWithDisabledPods = new ArrayList(); diff --git a/server/src/main/java/com/cloud/deploy/FirstFitPlanner.java b/server/src/main/java/com/cloud/deploy/FirstFitPlanner.java index b3593a02a134..8fe4d66ec020 100644 --- a/server/src/main/java/com/cloud/deploy/FirstFitPlanner.java +++ b/server/src/main/java/com/cloud/deploy/FirstFitPlanner.java @@ -562,7 +562,7 @@ private Pair, Map> getOrderedClustersByCapacity(long id logger.debug("CapacityType: {} is used for Cluster ordering", getCapacityTypeName(capacityType)); if (capacityType >= 0) { // for capacityType other than COMBINED - return capacityDao.orderClustersByAggregateCapacity(id, vmId, capacityType, isVr, isZone); + return capacityDao.orderClustersByAggregateCapacity(id, vmId, capacityType, isVr, allowRoutersOnDedicatedResources.value(), isZone); } Long zoneId = isZone ? id : null; @@ -695,6 +695,6 @@ public String getConfigComponentName() { @Override public ConfigKey[] getConfigKeys() { - return new ConfigKey[] {ClusterCPUCapacityDisableThreshold, ClusterMemoryCapacityDisableThreshold, ClusterThresholdEnabled, VmAllocationAlgorithm}; + return new ConfigKey[] {ClusterCPUCapacityDisableThreshold, ClusterMemoryCapacityDisableThreshold, ClusterThresholdEnabled, VmAllocationAlgorithm, allowRoutersOnDedicatedResources}; } } diff --git a/server/src/test/java/com/cloud/vm/FirstFitPlannerTest.java b/server/src/test/java/com/cloud/vm/FirstFitPlannerTest.java index 1742148c8143..4e26ba99d170 100644 --- a/server/src/test/java/com/cloud/vm/FirstFitPlannerTest.java +++ b/server/src/test/java/com/cloud/vm/FirstFitPlannerTest.java @@ -373,7 +373,7 @@ private void initializeForTest(VirtualMachineProfileImpl vmProfile, DataCenterDe clusterCapacityMap.put(6L, 2048D); Pair, Map> clustersOrderedByCapacity = new Pair, Map>(clustersWithEnoughCapacity, clusterCapacityMap); - when(capacityDao.orderClustersByAggregateCapacity(dataCenterId, 12L, Capacity.CAPACITY_TYPE_CPU, false, true)).thenReturn(clustersOrderedByCapacity); + when(capacityDao.orderClustersByAggregateCapacity(dataCenterId, 12L, Capacity.CAPACITY_TYPE_CPU, false, false,true)).thenReturn(clustersOrderedByCapacity); List disabledClusters = new ArrayList(); List clustersWithDisabledPods = new ArrayList(); From 2bc25d1577861761cb8bcab4f7fb5a71a7c13ddb Mon Sep 17 00:00:00 2001 From: Ortiga Date: Mon, 20 Apr 2026 14:55:10 -0300 Subject: [PATCH 3/3] allow deploy when resource is dedicated to account --- .../java/com/cloud/capacity/dao/CapacityDao.java | 2 +- .../java/com/cloud/capacity/dao/CapacityDaoImpl.java | 12 ++++++------ .../com/cloud/capacity/dao/CapacityDaoImplTest.java | 2 +- .../implicitplanner/ImplicitPlannerTest.java | 2 +- .../main/java/com/cloud/deploy/FirstFitPlanner.java | 11 ++++++----- .../test/java/com/cloud/vm/FirstFitPlannerTest.java | 2 +- 6 files changed, 16 insertions(+), 15 deletions(-) diff --git a/engine/schema/src/main/java/com/cloud/capacity/dao/CapacityDao.java b/engine/schema/src/main/java/com/cloud/capacity/dao/CapacityDao.java index ad24c4741b69..9e2bb0f0039c 100644 --- a/engine/schema/src/main/java/com/cloud/capacity/dao/CapacityDao.java +++ b/engine/schema/src/main/java/com/cloud/capacity/dao/CapacityDao.java @@ -41,7 +41,7 @@ public interface CapacityDao extends GenericDao { List findNonSharedStorageForClusterPodZone(Long zoneId, Long podId, Long clusterId); - Pair, Map> orderClustersByAggregateCapacity(long id, long vmId, short capacityType, boolean isVr, boolean allowRoutersOnDedicatedResources, boolean isZone); + Pair, Map> orderClustersByAggregateCapacity(long id, long vmId, Long ownerId, short capacityType, boolean isVr, boolean allowRoutersOnDedicatedResources, boolean isZone); Ternary findCapacityByZoneAndHostTag(Long zoneId, String hostTag); diff --git a/engine/schema/src/main/java/com/cloud/capacity/dao/CapacityDaoImpl.java b/engine/schema/src/main/java/com/cloud/capacity/dao/CapacityDaoImpl.java index 044d08e2472d..455fc929f09e 100644 --- a/engine/schema/src/main/java/com/cloud/capacity/dao/CapacityDaoImpl.java +++ b/engine/schema/src/main/java/com/cloud/capacity/dao/CapacityDaoImpl.java @@ -87,12 +87,12 @@ public class CapacityDaoImpl extends GenericDaoBase implements "LEFT JOIN dedicated_resources dr_cluster ON dr_cluster.cluster_id IS NOT NULL AND dr_cluster.cluster_id = host.cluster_id " + "LEFT JOIN dedicated_resources dr_host ON dr_host.host_id IS NOT NULL AND dr_host.host_id = host.id "; - private static final String ORDER_CLUSTERS_BY_AGGREGATE_CAPACITY_INCLUDE_DEDICATED_TO_DOMAIN_JOIN_1 = + private static final String ORDER_CLUSTERS_BY_AGGREGATE_CAPACITY_INCLUDE_DEDICATED_JOIN_1 = "JOIN host ON capacity.host_id = host.id " + "LEFT JOIN (SELECT affinity_group.id, agvm.instance_id FROM affinity_group_vm_map agvm JOIN affinity_group ON agvm.affinity_group_id = affinity_group.id AND affinity_group.type='ExplicitDedication') AS ag ON ag.instance_id = ? " + - "LEFT JOIN dedicated_resources dr_pod ON dr_pod.pod_id IS NOT NULL AND dr_pod.pod_id = host.pod_id AND dr_pod.account_id IS NOT NULL " + - "LEFT JOIN dedicated_resources dr_cluster ON dr_cluster.cluster_id IS NOT NULL AND dr_cluster.cluster_id = host.cluster_id AND dr_cluster.account_id IS NOT NULL " + - "LEFT JOIN dedicated_resources dr_host ON dr_host.host_id IS NOT NULL AND dr_host.host_id = host.id AND dr_host.account_id IS NOT NULL "; + "LEFT JOIN dedicated_resources dr_pod ON dr_pod.pod_id IS NOT NULL AND dr_pod.pod_id = host.pod_id AND dr_pod.account_id IS NOT NULL AND dr_pod.account_id != ownerId " + + "LEFT JOIN dedicated_resources dr_cluster ON dr_cluster.cluster_id IS NOT NULL AND dr_cluster.cluster_id = host.cluster_id AND dr_cluster.account_id IS NOT NULL AND dr_cluster.account_id != ownerId " + + "LEFT JOIN dedicated_resources dr_host ON dr_host.host_id IS NOT NULL AND dr_host.host_id = host.id AND dr_host.account_id IS NOT NULL AND dr_host.account_id != ownerId "; private static final String ORDER_CLUSTERS_BY_AGGREGATE_CAPACITY_JOIN_2 = " AND ((ag.id IS NULL AND dr_pod.pod_id IS NULL AND dr_cluster.cluster_id IS NULL AND dr_host.host_id IS NULL) OR " + @@ -993,7 +993,7 @@ public boolean removeBy(Short capacityType, Long zoneId, Long podId, Long cluste } @Override - public Pair, Map> orderClustersByAggregateCapacity(long id, long vmId, short capacityTypeForOrdering, boolean isVr, boolean allowRoutersOnDedicatedResources, boolean isZone) { + public Pair, Map> orderClustersByAggregateCapacity(long id, long vmId, Long ownerId, short capacityTypeForOrdering, boolean isVr, boolean allowRoutersOnDedicatedResources, boolean isZone) { TransactionLegacy txn = TransactionLegacy.currentTxn(); PreparedStatement pstmt = null; List result = new ArrayList(); @@ -1006,7 +1006,7 @@ public Pair, Map> orderClustersByAggregateCapacity(long } if (isVr && allowRoutersOnDedicatedResources) { - sql.append(ORDER_CLUSTERS_BY_AGGREGATE_CAPACITY_INCLUDE_DEDICATED_TO_DOMAIN_JOIN_1); + sql.append(ORDER_CLUSTERS_BY_AGGREGATE_CAPACITY_INCLUDE_DEDICATED_JOIN_1.replace("ownerId", ownerId.toString())); } else { sql.append(ORDER_CLUSTERS_BY_AGGREGATE_CAPACITY_JOIN_1); } diff --git a/engine/schema/src/test/java/com/cloud/capacity/dao/CapacityDaoImplTest.java b/engine/schema/src/test/java/com/cloud/capacity/dao/CapacityDaoImplTest.java index 57f48493c1e2..1ececb2ea769 100644 --- a/engine/schema/src/test/java/com/cloud/capacity/dao/CapacityDaoImplTest.java +++ b/engine/schema/src/test/java/com/cloud/capacity/dao/CapacityDaoImplTest.java @@ -250,7 +250,7 @@ public void testOrderClustersByAggregateCapacityEmptyResult() throws Exception { when(pstmt.executeQuery()).thenReturn(resultSet); when(resultSet.next()).thenReturn(false); - Pair, Map> result = capacityDao.orderClustersByAggregateCapacity(1L, 1L, (short) 1, false,true); + Pair, Map> result = capacityDao.orderClustersByAggregateCapacity(1L, 1L, 1L, (short) 1, false, false, true); assertNotNull(result); assertTrue(result.first().isEmpty()); assertTrue(result.second().isEmpty()); diff --git a/plugins/deployment-planners/implicit-dedication/src/test/java/org/apache/cloudstack/implicitplanner/ImplicitPlannerTest.java b/plugins/deployment-planners/implicit-dedication/src/test/java/org/apache/cloudstack/implicitplanner/ImplicitPlannerTest.java index 2346f67065f2..03b45c1c9a7d 100644 --- a/plugins/deployment-planners/implicit-dedication/src/test/java/org/apache/cloudstack/implicitplanner/ImplicitPlannerTest.java +++ b/plugins/deployment-planners/implicit-dedication/src/test/java/org/apache/cloudstack/implicitplanner/ImplicitPlannerTest.java @@ -366,7 +366,7 @@ private void initializeForTest(VirtualMachineProfileImpl vmProfile, DataCenterDe clusterCapacityMap.put(2L, 2048D); clusterCapacityMap.put(3L, 2048D); Pair, Map> clustersOrderedByCapacity = new Pair, Map>(clustersWithEnoughCapacity, clusterCapacityMap); - when(capacityDao.orderClustersByAggregateCapacity(dataCenterId, 12L, Capacity.CAPACITY_TYPE_CPU, false, false, true)).thenReturn(clustersOrderedByCapacity); + when(capacityDao.orderClustersByAggregateCapacity(dataCenterId, 12L, 1L, Capacity.CAPACITY_TYPE_CPU, false, false, true)).thenReturn(clustersOrderedByCapacity); List disabledClusters = new ArrayList(); List clustersWithDisabledPods = new ArrayList(); diff --git a/server/src/main/java/com/cloud/deploy/FirstFitPlanner.java b/server/src/main/java/com/cloud/deploy/FirstFitPlanner.java index 8fe4d66ec020..25e62225900f 100644 --- a/server/src/main/java/com/cloud/deploy/FirstFitPlanner.java +++ b/server/src/main/java/com/cloud/deploy/FirstFitPlanner.java @@ -399,9 +399,10 @@ private List scanClustersForDestinationInZoneOrPod(long id, boolean isZone int requiredCpu = offering.getCpu() * offering.getSpeed(); long requiredRam = offering.getRamSize() * 1024L * 1024L; boolean isVr = VirtualMachine.Type.DomainRouter.equals(vmProfile.getType()); + Long ownerId = vm.getAccountId(); //list clusters under this zone by cpu and ram capacity - Pair, Map> clusterCapacityInfo = listClustersByCapacity(id, vmProfile.getId(), requiredCpu, requiredRam, isVr, isZone); + Pair, Map> clusterCapacityInfo = listClustersByCapacity(id, vmProfile.getId(), ownerId, requiredCpu, requiredRam, isVr, isZone); List prioritizedClusterIds = clusterCapacityInfo.first(); if (!prioritizedClusterIds.isEmpty()) { if (avoid.getClustersToAvoid() != null) { @@ -459,7 +460,7 @@ protected List reorderPods(Pair, Map> podCapacity return podIdsByCapacity; } - protected Pair, Map> listClustersByCapacity(long id, long vmId, int requiredCpu, long requiredRam, boolean isVr, boolean isZone) { + protected Pair, Map> listClustersByCapacity(long id, long vmId, Long ownerId, int requiredCpu, long requiredRam, boolean isVr, boolean isZone) { //look at the aggregate available cpu and ram per cluster //although an aggregate value may be false indicator that a cluster can host a vm, it will at the least eliminate those clusters which definitely cannot @@ -475,7 +476,7 @@ protected Pair, Map> listClustersByCapacity(long id, lo } - Pair, Map> result = getOrderedClustersByCapacity(id, vmId, isVr, isZone); + Pair, Map> result = getOrderedClustersByCapacity(id, vmId, ownerId, isVr, isZone); List clusterIdsOrderedByAggregateCapacity = result.first(); //only keep the clusters that have enough capacity to host this VM if (logger.isTraceEnabled()) { @@ -555,14 +556,14 @@ public Map getPodByCombinedCapacities(List capacities, } - private Pair, Map> getOrderedClustersByCapacity(long id, long vmId, boolean isVr, boolean isZone) { + private Pair, Map> getOrderedClustersByCapacity(long id, long vmId, Long ownerId, boolean isVr, boolean isZone) { double cpuToMemoryWeight = ConfigurationManager.HostCapacityTypeCpuMemoryWeight.value(); short capacityType = getHostCapacityTypeToOrderCluster( configDao.getValue(Config.HostCapacityTypeToOrderClusters.key()), cpuToMemoryWeight); logger.debug("CapacityType: {} is used for Cluster ordering", getCapacityTypeName(capacityType)); if (capacityType >= 0) { // for capacityType other than COMBINED - return capacityDao.orderClustersByAggregateCapacity(id, vmId, capacityType, isVr, allowRoutersOnDedicatedResources.value(), isZone); + return capacityDao.orderClustersByAggregateCapacity(id, vmId, ownerId, capacityType, isVr, allowRoutersOnDedicatedResources.value(), isZone); } Long zoneId = isZone ? id : null; diff --git a/server/src/test/java/com/cloud/vm/FirstFitPlannerTest.java b/server/src/test/java/com/cloud/vm/FirstFitPlannerTest.java index 4e26ba99d170..43b917886b03 100644 --- a/server/src/test/java/com/cloud/vm/FirstFitPlannerTest.java +++ b/server/src/test/java/com/cloud/vm/FirstFitPlannerTest.java @@ -373,7 +373,7 @@ private void initializeForTest(VirtualMachineProfileImpl vmProfile, DataCenterDe clusterCapacityMap.put(6L, 2048D); Pair, Map> clustersOrderedByCapacity = new Pair, Map>(clustersWithEnoughCapacity, clusterCapacityMap); - when(capacityDao.orderClustersByAggregateCapacity(dataCenterId, 12L, Capacity.CAPACITY_TYPE_CPU, false, false,true)).thenReturn(clustersOrderedByCapacity); + when(capacityDao.orderClustersByAggregateCapacity(dataCenterId, 12L, 1L, Capacity.CAPACITY_TYPE_CPU, false, false, true)).thenReturn(clustersOrderedByCapacity); List disabledClusters = new ArrayList(); List clustersWithDisabledPods = new ArrayList();