7373import com .cloud .user .AccountManager ;
7474import com .cloud .utils .NumbersUtil ;
7575import com .cloud .utils .Pair ;
76+ import com .cloud .utils .StringUtils ;
7677import com .cloud .utils .component .Adapters ;
7778import com .cloud .utils .component .Inject ;
7879import com .cloud .vm .DiskProfile ;
@@ -457,7 +458,7 @@ private List<Short> getCapacitiesForCheckingThreshold(){
457458 return capacityList ;
458459 }
459460
460- private void removeClustersCrossingThreshold (List <Long > clusterList , ExcludeList avoid , VirtualMachineProfile <? extends VirtualMachine > vmProfile ){
461+ private void removeClustersCrossingThreshold (List <Long > clusterListForVmAllocation , ExcludeList avoid , VirtualMachineProfile <? extends VirtualMachine > vmProfile , DeploymentPlan plan ){
461462
462463 Map <Short ,Float > capacityThresholdMap = getCapacityThresholdMap ();
463464 List <Short > capacityList = getCapacitiesForCheckingThreshold ();
@@ -467,37 +468,33 @@ private void removeClustersCrossingThreshold(List<Long> clusterList, ExcludeList
467468 int cpu_requested = offering .getCpu () * offering .getSpeed ();
468469 long ram_requested = offering .getRamSize () * 1024L * 1024L ;
469470
470- // Iterate over the cluster List and check for each cluster whether it breaks disable threshold for any of the capacity types
471- for (Long clusterId : clusterList ){
472- for (short capacity : capacityList ){
473-
474- List <SummedCapacity > summedCapacityList = _capacityDao .findCapacityBy (new Integer (capacity ), null , null , clusterId );
475- if (summedCapacityList != null && summedCapacityList .size () != 0 && summedCapacityList .get (0 ).getTotalCapacity () != 0 ){
476-
477- double used = (double )(summedCapacityList .get (0 ).getUsedCapacity () + summedCapacityList .get (0 ).getReservedCapacity ());
478- double total = summedCapacityList .get (0 ).getTotalCapacity ();
479-
480- if (capacity == Capacity .CAPACITY_TYPE_CPU ){
481- total = total * ApiDBUtils .getCpuOverprovisioningFactor ();
482- used = used + cpu_requested ;
483- }else {
484- used = used + ram_requested ;
485- }
486-
487- double usedPercentage = used /total ;
488- if ( usedPercentage > capacityThresholdMap .get (capacity )){
489- avoid .addCluster (clusterId );
490- clustersCrossingThreshold .add (clusterId );
491- s_logger .debug ("Cannot allocate cluster " + clusterId + " for vm creation since its allocated percentage: " +usedPercentage +
492- " will cross the disable capacity threshold: " + capacityThresholdMap .get (capacity ) + " for capacity Type : " + capacity + ", skipping this cluster" );
493- break ;
494- }
495- }
496- }
497- }
498-
499- clusterList .removeAll (clustersCrossingThreshold );
500-
471+ // For each capacity get the cluster list crossing the threshold and remove it from the clusterList that will be used for vm allocation.
472+ for (short capacity : capacityList ){
473+
474+ if (clusterListForVmAllocation == null || clusterListForVmAllocation .size () == 0 ){
475+ return ;
476+ }
477+
478+ if (capacity == Capacity .CAPACITY_TYPE_CPU ){
479+ clustersCrossingThreshold = _capacityDao .listClustersCrossingThreshold (Capacity .CAPACITY_TYPE_CPU , plan .getDataCenterId (),
480+ capacityThresholdMap .get (capacity ), cpu_requested , ApiDBUtils .getCpuOverprovisioningFactor ());
481+ }else {
482+ clustersCrossingThreshold = _capacityDao .listClustersCrossingThreshold (capacity , plan .getDataCenterId (),
483+ capacityThresholdMap .get (capacity ), ram_requested , 1.0f );//Mem overprov not supported yet
484+ }
485+
486+
487+ if (clustersCrossingThreshold != null && clustersCrossingThreshold .size () != 0 ){
488+ // addToAvoid Set
489+ avoid .addClusterList (clustersCrossingThreshold );
490+ // Remove clusters crossing disabled threshold
491+ clusterListForVmAllocation .removeAll (clustersCrossingThreshold );
492+
493+ s_logger .debug ("Cannot allocate cluster list " + clustersCrossingThreshold .toString () + " for vm creation since their allocated percentage" +
494+ " crosses the disable capacity threshold: " + capacityThresholdMap .get (capacity ) + " for capacity Type : " + capacity + ", skipping these clusters" );
495+ }
496+
497+ }
501498 }
502499
503500 private DeployDestination checkClustersforDestination (List <Long > clusterList , VirtualMachineProfile <? extends VirtualMachine > vmProfile ,
@@ -507,7 +504,7 @@ private DeployDestination checkClustersforDestination(List<Long> clusterList, Vi
507504 s_logger .trace ("ClusterId List to consider: " + clusterList );
508505 }
509506
510- removeClustersCrossingThreshold (clusterList , avoid , vmProfile );
507+ removeClustersCrossingThreshold (clusterList , avoid , vmProfile , plan );
511508
512509 for (Long clusterId : clusterList ){
513510 Cluster clusterVO = _clusterDao .findById (clusterId );
0 commit comments