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 ;
4041import org .apache .log4j .Logger ;
4142
4243import com .cloud .agent .AgentManager ;
6566import com .cloud .service .ServiceOfferingVO ;
6667import com .cloud .service .dao .ServiceOfferingDao ;
6768import com .cloud .utils .Pair ;
69+ import com .cloud .utils .StringUtils ;
6870import com .cloud .utils .Ternary ;
6971import com .cloud .utils .component .ManagerBase ;
7072import com .cloud .utils .exception .CloudRuntimeException ;
73+ import com .cloud .vm .UserVmDetailVO ;
7174import com .cloud .vm .VMInstanceVO ;
7275import com .cloud .vm .VirtualMachine .State ;
7376import com .cloud .vm .VirtualMachineProfileImpl ;
77+ import com .cloud .vm .VmDetailConstants ;
78+ import com .cloud .vm .dao .UserVmDetailsDao ;
7479import com .cloud .vm .dao .VMInstanceDao ;
7580
7681public class RollingMaintenanceManagerImpl extends ManagerBase implements RollingMaintenanceManager {
@@ -86,6 +91,8 @@ public class RollingMaintenanceManagerImpl extends ManagerBase implements Rollin
8691 @ Inject
8792 private VMInstanceDao vmInstanceDao ;
8893 @ Inject
94+ protected UserVmDetailsDao userVmDetailsDao ;
95+ @ Inject
8996 private ServiceOfferingDao serviceOfferingDao ;
9097 @ Inject
9198 private ClusterDetailsDao clusterDetailsDao ;
@@ -621,10 +628,19 @@ private Pair<Boolean, String> performCapacityChecksBeforeHostInMaintenance(Host
621628 int successfullyCheckedVmMigrations = 0 ;
622629 for (VMInstanceVO runningVM : vmsRunning ) {
623630 boolean canMigrateVm = false ;
631+ Ternary <Integer , Integer , Integer > cpuSpeedAndRamSize = getComputeResourcesCpuSpeedAndRamSize (runningVM );
632+ Integer cpu = cpuSpeedAndRamSize .first ();
633+ Integer speed = cpuSpeedAndRamSize .second ();
634+ Integer ramSize = cpuSpeedAndRamSize .third ();
635+ if (ObjectUtils .anyNull (cpu , speed , ramSize )) {
636+ s_logger .warn (String .format ("Cannot fetch compute resources for the VM %s, skipping it from the capacity check" , runningVM ));
637+ continue ;
638+ }
639+
624640 ServiceOfferingVO serviceOffering = serviceOfferingDao .findById (runningVM .getServiceOfferingId ());
625641 for (Host hostInCluster : hostsInCluster ) {
626642 if (!checkHostTags (hostTags , hostTagsDao .getHostTags (hostInCluster .getId ()), serviceOffering .getHostTag ())) {
627- s_logger .debug (String .format ("Host tags mismatch between %s and %s Skipping it from the capacity check" , host , hostInCluster ));
643+ s_logger .warn (String .format ("Host tags mismatch between %s and %s, skipping it from the capacity check" , host , hostInCluster ));
628644 continue ;
629645 }
630646 DeployDestination deployDestination = new DeployDestination (null , null , null , host );
@@ -634,13 +650,13 @@ private Pair<Boolean, String> performCapacityChecksBeforeHostInMaintenance(Host
634650 affinityChecks = affinityChecks && affinityProcessor .check (vmProfile , deployDestination );
635651 }
636652 if (!affinityChecks ) {
637- s_logger .debug (String .format ("Affinity check failed between %s and %s Skipping it from the capacity check" , host , hostInCluster ));
653+ s_logger .warn (String .format ("Affinity check failed between %s and %s, skipping it from the capacity check" , host , hostInCluster ));
638654 continue ;
639655 }
640656 boolean maxGuestLimit = capacityManager .checkIfHostReachMaxGuestLimit (host );
641- boolean hostHasCPUCapacity = capacityManager .checkIfHostHasCpuCapability (hostInCluster .getId (), serviceOffering . getCpu (), serviceOffering . getSpeed () );
642- int cpuRequested = serviceOffering . getCpu () * serviceOffering . getSpeed () ;
643- long ramRequested = serviceOffering . getRamSize () * 1024L * 1024L ;
657+ boolean hostHasCPUCapacity = capacityManager .checkIfHostHasCpuCapability (hostInCluster .getId (), cpu , speed );
658+ int cpuRequested = cpu * speed ;
659+ long ramRequested = ramSize * 1024L * 1024L ;
644660 ClusterDetailsVO clusterDetailsCpuOvercommit = clusterDetailsDao .findDetail (cluster .getId (), "cpuOvercommitRatio" );
645661 ClusterDetailsVO clusterDetailsRamOvercommmt = clusterDetailsDao .findDetail (cluster .getId (), "memoryOvercommitRatio" );
646662 Float cpuOvercommitRatio = Float .parseFloat (clusterDetailsCpuOvercommit .getValue ());
@@ -666,11 +682,42 @@ private Pair<Boolean, String> performCapacityChecksBeforeHostInMaintenance(Host
666682 return new Pair <>(true , "OK" );
667683 }
668684
685+ protected Ternary <Integer , Integer , Integer > getComputeResourcesCpuSpeedAndRamSize (VMInstanceVO runningVM ) {
686+ ServiceOfferingVO serviceOffering = serviceOfferingDao .findById (runningVM .getServiceOfferingId ());
687+ Integer cpu = serviceOffering .getCpu ();
688+ Integer speed = serviceOffering .getSpeed ();
689+ Integer ramSize = serviceOffering .getRamSize ();
690+ if (!serviceOffering .isDynamic ()) {
691+ return new Ternary <>(cpu , speed , ramSize );
692+ }
693+
694+ List <UserVmDetailVO > vmDetails = userVmDetailsDao .listDetails (runningVM .getId ());
695+ if (CollectionUtils .isEmpty (vmDetails )) {
696+ return new Ternary <>(cpu , speed , ramSize );
697+ }
698+
699+ for (UserVmDetailVO vmDetail : vmDetails ) {
700+ if (StringUtils .isBlank (vmDetail .getName ()) || StringUtils .isBlank (vmDetail .getValue ())) {
701+ continue ;
702+ }
703+
704+ if (cpu == null && VmDetailConstants .CPU_NUMBER .equals (vmDetail .getName ())) {
705+ cpu = Integer .valueOf (vmDetail .getValue ());
706+ } else if (speed == null && VmDetailConstants .CPU_SPEED .equals (vmDetail .getName ())) {
707+ speed = Integer .valueOf (vmDetail .getValue ());
708+ } else if (ramSize == null && VmDetailConstants .MEMORY .equals (vmDetail .getName ())) {
709+ ramSize = Integer .valueOf (vmDetail .getValue ());
710+ }
711+ }
712+
713+ return new Ternary <>(cpu , speed , ramSize );
714+ }
715+
669716 /**
670717 * Check hosts tags
671718 */
672719 private boolean checkHostTags (List <HostTagVO > hostTags , List <HostTagVO > hostInClusterTags , String offeringTag ) {
673- if (CollectionUtils .isEmpty (hostTags ) && CollectionUtils .isEmpty (hostInClusterTags )) {
720+ if (( CollectionUtils .isEmpty (hostTags ) && CollectionUtils .isEmpty (hostInClusterTags )) || StringUtils . isBlank ( offeringTag )) {
674721 return true ;
675722 } else if ((CollectionUtils .isNotEmpty (hostTags ) && CollectionUtils .isEmpty (hostInClusterTags )) ||
676723 (CollectionUtils .isEmpty (hostTags ) && CollectionUtils .isNotEmpty (hostInClusterTags ))) {
0 commit comments