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 ;
41+ import org .apache .log4j .Logger ;
4042
4143import com .cloud .agent .AgentManager ;
4244import com .cloud .agent .api .Answer ;
6466import com .cloud .service .ServiceOfferingVO ;
6567import com .cloud .service .dao .ServiceOfferingDao ;
6668import com .cloud .utils .Pair ;
69+ import com .cloud .utils .StringUtils ;
6770import com .cloud .utils .Ternary ;
6871import com .cloud .utils .component .ManagerBase ;
6972import com .cloud .utils .exception .CloudRuntimeException ;
73+ import com .cloud .vm .UserVmDetailVO ;
7074import com .cloud .vm .VMInstanceVO ;
7175import com .cloud .vm .VirtualMachine .State ;
7276import com .cloud .vm .VirtualMachineProfileImpl ;
77+ import com .cloud .vm .VmDetailConstants ;
78+ import com .cloud .vm .dao .UserVmDetailsDao ;
7379import com .cloud .vm .dao .VMInstanceDao ;
7480
7581public class RollingMaintenanceManagerImpl extends ManagerBase implements RollingMaintenanceManager {
@@ -85,6 +91,8 @@ public class RollingMaintenanceManagerImpl extends ManagerBase implements Rollin
8591 @ Inject
8692 private VMInstanceDao vmInstanceDao ;
8793 @ Inject
94+ protected UserVmDetailsDao userVmDetailsDao ;
95+ @ Inject
8896 private ServiceOfferingDao serviceOfferingDao ;
8997 @ Inject
9098 private ClusterDetailsDao clusterDetailsDao ;
@@ -619,10 +627,19 @@ private Pair<Boolean, String> performCapacityChecksBeforeHostInMaintenance(Host
619627 int successfullyCheckedVmMigrations = 0 ;
620628 for (VMInstanceVO runningVM : vmsRunning ) {
621629 boolean canMigrateVm = false ;
630+ Ternary <Integer , Integer , Integer > cpuSpeedAndRamSize = getComputeResourcesCpuSpeedAndRamSize (runningVM );
631+ Integer cpu = cpuSpeedAndRamSize .first ();
632+ Integer speed = cpuSpeedAndRamSize .second ();
633+ Integer ramSize = cpuSpeedAndRamSize .third ();
634+ if (ObjectUtils .anyNull (cpu , speed , ramSize )) {
635+ logger .warn (String .format ("Cannot fetch compute resources for the VM %s, skipping it from the capacity check" , runningVM ));
636+ continue ;
637+ }
638+
622639 ServiceOfferingVO serviceOffering = serviceOfferingDao .findById (runningVM .getServiceOfferingId ());
623640 for (Host hostInCluster : hostsInCluster ) {
624641 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 ));
642+ logger .warn (String .format ("Host tags mismatch between %s and %s, skipping it from the capacity check" , host , hostInCluster ));
626643 continue ;
627644 }
628645 DeployDestination deployDestination = new DeployDestination (null , null , null , host );
@@ -632,13 +649,13 @@ private Pair<Boolean, String> performCapacityChecksBeforeHostInMaintenance(Host
632649 affinityChecks = affinityChecks && affinityProcessor .check (vmProfile , deployDestination );
633650 }
634651 if (!affinityChecks ) {
635- logger .debug (String .format ("Affinity check failed between %s and %s Skipping it from the capacity check" , host , hostInCluster ));
652+ logger .warn (String .format ("Affinity check failed between %s and %s, skipping it from the capacity check" , host , hostInCluster ));
636653 continue ;
637654 }
638655 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 ;
656+ boolean hostHasCPUCapacity = capacityManager .checkIfHostHasCpuCapability (hostInCluster .getId (), cpu , speed );
657+ int cpuRequested = cpu * speed ;
658+ long ramRequested = ramSize * 1024L * 1024L ;
642659 ClusterDetailsVO clusterDetailsCpuOvercommit = clusterDetailsDao .findDetail (cluster .getId (), "cpuOvercommitRatio" );
643660 ClusterDetailsVO clusterDetailsRamOvercommmt = clusterDetailsDao .findDetail (cluster .getId (), "memoryOvercommitRatio" );
644661 Float cpuOvercommitRatio = Float .parseFloat (clusterDetailsCpuOvercommit .getValue ());
@@ -664,11 +681,42 @@ private Pair<Boolean, String> performCapacityChecksBeforeHostInMaintenance(Host
664681 return new Pair <>(true , "OK" );
665682 }
666683
684+ protected Ternary <Integer , Integer , Integer > getComputeResourcesCpuSpeedAndRamSize (VMInstanceVO runningVM ) {
685+ ServiceOfferingVO serviceOffering = serviceOfferingDao .findById (runningVM .getServiceOfferingId ());
686+ Integer cpu = serviceOffering .getCpu ();
687+ Integer speed = serviceOffering .getSpeed ();
688+ Integer ramSize = serviceOffering .getRamSize ();
689+ if (!serviceOffering .isDynamic ()) {
690+ return new Ternary <>(cpu , speed , ramSize );
691+ }
692+
693+ List <UserVmDetailVO > vmDetails = userVmDetailsDao .listDetails (runningVM .getId ());
694+ if (CollectionUtils .isEmpty (vmDetails )) {
695+ return new Ternary <>(cpu , speed , ramSize );
696+ }
697+
698+ for (UserVmDetailVO vmDetail : vmDetails ) {
699+ if (StringUtils .isBlank (vmDetail .getName ()) || StringUtils .isBlank (vmDetail .getValue ())) {
700+ continue ;
701+ }
702+
703+ if (cpu == null && VmDetailConstants .CPU_NUMBER .equals (vmDetail .getName ())) {
704+ cpu = Integer .valueOf (vmDetail .getValue ());
705+ } else if (speed == null && VmDetailConstants .CPU_SPEED .equals (vmDetail .getName ())) {
706+ speed = Integer .valueOf (vmDetail .getValue ());
707+ } else if (ramSize == null && VmDetailConstants .MEMORY .equals (vmDetail .getName ())) {
708+ ramSize = Integer .valueOf (vmDetail .getValue ());
709+ }
710+ }
711+
712+ return new Ternary <>(cpu , speed , ramSize );
713+ }
714+
667715 /**
668716 * Check hosts tags
669717 */
670718 private boolean checkHostTags (List <HostTagVO > hostTags , List <HostTagVO > hostInClusterTags , String offeringTag ) {
671- if (CollectionUtils .isEmpty (hostTags ) && CollectionUtils .isEmpty (hostInClusterTags )) {
719+ if (( CollectionUtils .isEmpty (hostTags ) && CollectionUtils .isEmpty (hostInClusterTags )) || StringUtils . isBlank ( offeringTag )) {
672720 return true ;
673721 } else if ((CollectionUtils .isNotEmpty (hostTags ) && CollectionUtils .isEmpty (hostInClusterTags )) ||
674722 (CollectionUtils .isEmpty (hostTags ) && CollectionUtils .isNotEmpty (hostInClusterTags ))) {
0 commit comments