3737import org .apache .cloudstack .context .CallContext ;
3838import org .apache .cloudstack .framework .config .ConfigKey ;
3939import org .apache .commons .collections .CollectionUtils ;
40+ import org .apache .commons .lang3 .ObjectUtils ;
4041
4142import com .cloud .agent .AgentManager ;
4243import com .cloud .agent .api .Answer ;
6465import com .cloud .service .ServiceOfferingVO ;
6566import com .cloud .service .dao .ServiceOfferingDao ;
6667import com .cloud .utils .Pair ;
68+ import com .cloud .utils .StringUtils ;
6769import com .cloud .utils .Ternary ;
6870import com .cloud .utils .component .ManagerBase ;
6971import com .cloud .utils .exception .CloudRuntimeException ;
72+ import com .cloud .vm .UserVmDetailVO ;
7073import com .cloud .vm .VMInstanceVO ;
7174import com .cloud .vm .VirtualMachine .State ;
7275import com .cloud .vm .VirtualMachineProfileImpl ;
76+ import com .cloud .vm .VmDetailConstants ;
77+ import com .cloud .vm .dao .UserVmDetailsDao ;
7378import com .cloud .vm .dao .VMInstanceDao ;
7479
7580public class RollingMaintenanceManagerImpl extends ManagerBase implements RollingMaintenanceManager {
@@ -85,6 +90,8 @@ public class RollingMaintenanceManagerImpl extends ManagerBase implements Rollin
8590 @ Inject
8691 private VMInstanceDao vmInstanceDao ;
8792 @ Inject
93+ protected UserVmDetailsDao userVmDetailsDao ;
94+ @ Inject
8895 private ServiceOfferingDao serviceOfferingDao ;
8996 @ Inject
9097 private ClusterDetailsDao clusterDetailsDao ;
@@ -619,10 +626,19 @@ private Pair<Boolean, String> performCapacityChecksBeforeHostInMaintenance(Host
619626 int successfullyCheckedVmMigrations = 0 ;
620627 for (VMInstanceVO runningVM : vmsRunning ) {
621628 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+
622638 ServiceOfferingVO serviceOffering = serviceOfferingDao .findById (runningVM .getServiceOfferingId ());
623639 for (Host hostInCluster : hostsInCluster ) {
624640 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 );
626642 continue ;
627643 }
628644 DeployDestination deployDestination = new DeployDestination (null , null , null , host );
@@ -632,13 +648,13 @@ private Pair<Boolean, String> performCapacityChecksBeforeHostInMaintenance(Host
632648 affinityChecks = affinityChecks && affinityProcessor .check (vmProfile , deployDestination );
633649 }
634650 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 );
636652 continue ;
637653 }
638654 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 ;
642658 ClusterDetailsVO clusterDetailsCpuOvercommit = clusterDetailsDao .findDetail (cluster .getId (), "cpuOvercommitRatio" );
643659 ClusterDetailsVO clusterDetailsRamOvercommmt = clusterDetailsDao .findDetail (cluster .getId (), "memoryOvercommitRatio" );
644660 Float cpuOvercommitRatio = Float .parseFloat (clusterDetailsCpuOvercommit .getValue ());
@@ -664,11 +680,42 @@ private Pair<Boolean, String> performCapacityChecksBeforeHostInMaintenance(Host
664680 return new Pair <>(true , "OK" );
665681 }
666682
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+
667714 /**
668715 * Check hosts tags
669716 */
670717 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 )) {
672719 return true ;
673720 } else if ((CollectionUtils .isNotEmpty (hostTags ) && CollectionUtils .isEmpty (hostInClusterTags )) ||
674721 (CollectionUtils .isEmpty (hostTags ) && CollectionUtils .isNotEmpty (hostInClusterTags ))) {
0 commit comments