37
37
import org .apache .cloudstack .context .CallContext ;
38
38
import org .apache .cloudstack .framework .config .ConfigKey ;
39
39
import org .apache .commons .collections .CollectionUtils ;
40
+ import org .apache .commons .lang3 .ObjectUtils ;
40
41
41
42
import com .cloud .agent .AgentManager ;
42
43
import com .cloud .agent .api .Answer ;
64
65
import com .cloud .service .ServiceOfferingVO ;
65
66
import com .cloud .service .dao .ServiceOfferingDao ;
66
67
import com .cloud .utils .Pair ;
68
+ import com .cloud .utils .StringUtils ;
67
69
import com .cloud .utils .Ternary ;
68
70
import com .cloud .utils .component .ManagerBase ;
69
71
import com .cloud .utils .exception .CloudRuntimeException ;
72
+ import com .cloud .vm .UserVmDetailVO ;
70
73
import com .cloud .vm .VMInstanceVO ;
71
74
import com .cloud .vm .VirtualMachine .State ;
72
75
import com .cloud .vm .VirtualMachineProfileImpl ;
76
+ import com .cloud .vm .VmDetailConstants ;
77
+ import com .cloud .vm .dao .UserVmDetailsDao ;
73
78
import com .cloud .vm .dao .VMInstanceDao ;
74
79
75
80
public class RollingMaintenanceManagerImpl extends ManagerBase implements RollingMaintenanceManager {
@@ -85,6 +90,8 @@ public class RollingMaintenanceManagerImpl extends ManagerBase implements Rollin
85
90
@ Inject
86
91
private VMInstanceDao vmInstanceDao ;
87
92
@ Inject
93
+ protected UserVmDetailsDao userVmDetailsDao ;
94
+ @ Inject
88
95
private ServiceOfferingDao serviceOfferingDao ;
89
96
@ Inject
90
97
private ClusterDetailsDao clusterDetailsDao ;
@@ -619,10 +626,19 @@ private Pair<Boolean, String> performCapacityChecksBeforeHostInMaintenance(Host
619
626
int successfullyCheckedVmMigrations = 0 ;
620
627
for (VMInstanceVO runningVM : vmsRunning ) {
621
628
boolean canMigrateVm = false ;
629
+ Ternary <Integer , Integer , Integer > cpuSpeedAndRamSize = getComputeResourcesCpuSpeedAndRamSize (runningVM );
630
+ Integer cpu = cpuSpeedAndRamSize .first ();
631
+ Integer speed = cpuSpeedAndRamSize .second ();
632
+ Integer ramSize = cpuSpeedAndRamSize .third ();
633
+ if (ObjectUtils .anyNull (cpu , speed , ramSize )) {
634
+ logger .warn ("Cannot fetch compute resources for the VM {}, skipping it from the capacity check" , runningVM );
635
+ continue ;
636
+ }
637
+
622
638
ServiceOfferingVO serviceOffering = serviceOfferingDao .findById (runningVM .getServiceOfferingId ());
623
639
for (Host hostInCluster : hostsInCluster ) {
624
640
if (!checkHostTags (hostTags , hostTagsDao .getHostTags (hostInCluster .getId ()), serviceOffering .getHostTag ())) {
625
- logger .debug (String . format ( "Host tags mismatch between %s and %s Skipping it from the capacity check" , host , hostInCluster ) );
641
+ logger .debug ("Host tags mismatch between {} and {} Skipping it from the capacity check" , host , hostInCluster );
626
642
continue ;
627
643
}
628
644
DeployDestination deployDestination = new DeployDestination (null , null , null , host );
@@ -632,13 +648,13 @@ private Pair<Boolean, String> performCapacityChecksBeforeHostInMaintenance(Host
632
648
affinityChecks = affinityChecks && affinityProcessor .check (vmProfile , deployDestination );
633
649
}
634
650
if (!affinityChecks ) {
635
- logger .debug (String . format ( "Affinity check failed between %s and %s Skipping it from the capacity check" , host , hostInCluster ) );
651
+ logger .debug ("Affinity check failed between {} and {} Skipping it from the capacity check" , host , hostInCluster );
636
652
continue ;
637
653
}
638
654
boolean maxGuestLimit = capacityManager .checkIfHostReachMaxGuestLimit (host );
639
- boolean hostHasCPUCapacity = capacityManager .checkIfHostHasCpuCapability (hostInCluster .getId (), serviceOffering . getCpu (), serviceOffering . getSpeed () );
640
- int cpuRequested = serviceOffering . getCpu () * serviceOffering . getSpeed () ;
641
- long ramRequested = serviceOffering . getRamSize () * 1024L * 1024L ;
655
+ boolean hostHasCPUCapacity = capacityManager .checkIfHostHasCpuCapability (hostInCluster .getId (), cpu , speed );
656
+ int cpuRequested = cpu * speed ;
657
+ long ramRequested = ramSize * 1024L * 1024L ;
642
658
ClusterDetailsVO clusterDetailsCpuOvercommit = clusterDetailsDao .findDetail (cluster .getId (), "cpuOvercommitRatio" );
643
659
ClusterDetailsVO clusterDetailsRamOvercommmt = clusterDetailsDao .findDetail (cluster .getId (), "memoryOvercommitRatio" );
644
660
Float cpuOvercommitRatio = Float .parseFloat (clusterDetailsCpuOvercommit .getValue ());
@@ -664,11 +680,42 @@ private Pair<Boolean, String> performCapacityChecksBeforeHostInMaintenance(Host
664
680
return new Pair <>(true , "OK" );
665
681
}
666
682
683
+ protected Ternary <Integer , Integer , Integer > getComputeResourcesCpuSpeedAndRamSize (VMInstanceVO runningVM ) {
684
+ ServiceOfferingVO serviceOffering = serviceOfferingDao .findById (runningVM .getServiceOfferingId ());
685
+ Integer cpu = serviceOffering .getCpu ();
686
+ Integer speed = serviceOffering .getSpeed ();
687
+ Integer ramSize = serviceOffering .getRamSize ();
688
+ if (!serviceOffering .isDynamic ()) {
689
+ return new Ternary <>(cpu , speed , ramSize );
690
+ }
691
+
692
+ List <UserVmDetailVO > vmDetails = userVmDetailsDao .listDetails (runningVM .getId ());
693
+ if (CollectionUtils .isEmpty (vmDetails )) {
694
+ return new Ternary <>(cpu , speed , ramSize );
695
+ }
696
+
697
+ for (UserVmDetailVO vmDetail : vmDetails ) {
698
+ if (StringUtils .isBlank (vmDetail .getName ()) || StringUtils .isBlank (vmDetail .getValue ())) {
699
+ continue ;
700
+ }
701
+
702
+ if (cpu == null && VmDetailConstants .CPU_NUMBER .equals (vmDetail .getName ())) {
703
+ cpu = Integer .valueOf (vmDetail .getValue ());
704
+ } else if (speed == null && VmDetailConstants .CPU_SPEED .equals (vmDetail .getName ())) {
705
+ speed = Integer .valueOf (vmDetail .getValue ());
706
+ } else if (ramSize == null && VmDetailConstants .MEMORY .equals (vmDetail .getName ())) {
707
+ ramSize = Integer .valueOf (vmDetail .getValue ());
708
+ }
709
+ }
710
+
711
+ return new Ternary <>(cpu , speed , ramSize );
712
+ }
713
+
667
714
/**
668
715
* Check hosts tags
669
716
*/
670
717
private boolean checkHostTags (List <HostTagVO > hostTags , List <HostTagVO > hostInClusterTags , String offeringTag ) {
671
- if (CollectionUtils .isEmpty (hostTags ) && CollectionUtils .isEmpty (hostInClusterTags )) {
718
+ if (( CollectionUtils .isEmpty (hostTags ) && CollectionUtils .isEmpty (hostInClusterTags )) || StringUtils . isBlank ( offeringTag )) {
672
719
return true ;
673
720
} else if ((CollectionUtils .isNotEmpty (hostTags ) && CollectionUtils .isEmpty (hostInClusterTags )) ||
674
721
(CollectionUtils .isEmpty (hostTags ) && CollectionUtils .isNotEmpty (hostInClusterTags ))) {
0 commit comments