@@ -379,14 +379,12 @@ public DeployDestination planDeployment(VirtualMachineProfile vmProfile, Deploym
379379 planner = getDeploymentPlannerByName (plannerName );
380380 }
381381
382- Host lastHost = checkDeployInVmLastHost (vmProfile , vm );
383- if (lastHost != null ) {
384- DeployDestination deployDestination = deployInVmLastHost (vmProfile , plan , avoids , planner , vm , dc , offering , cpuRequested , ramRequested , volumesRequireEncryption );
385- if (deployDestination != null ) {
386- return deployDestination ;
387- }
382+ DeployDestination deployDestinationForVmLasthost = deployInVmLastHost (vmProfile , plan , avoids , planner , vm , dc , offering , cpuRequested , ramRequested , volumesRequireEncryption );
383+ if (deployDestinationForVmLasthost != null ) {
384+ return deployDestinationForVmLasthost ;
388385 }
389386
387+ HostVO lastHost = _hostDao .findById (vm .getLastHostId ());
390388 avoidOtherClustersForDeploymentIfMigrationDisabled (vm , lastHost , avoids );
391389
392390 DeployDestination dest = null ;
@@ -449,31 +447,6 @@ public DeployDestination planDeployment(VirtualMachineProfile vmProfile, Deploym
449447 return dest ;
450448 }
451449
452- private Host checkDeployInVmLastHost (VirtualMachineProfile vmProfile , VirtualMachine vm ) {
453- String considerLastHostStr = (String )vmProfile .getParameter (VirtualMachineProfile .Param .ConsiderLastHost );
454- String haVmTag = (String )vmProfile .getParameter (VirtualMachineProfile .Param .HaTag );
455- boolean considerLastHost = vm .getLastHostId () != null && haVmTag == null &&
456- (considerLastHostStr == null || Boolean .TRUE .toString ().equalsIgnoreCase (considerLastHostStr ));
457- if (!considerLastHost ) {
458- return null ;
459- }
460-
461- logger .debug ("This VM has last host_id: {}" , vm .getLastHostId ());
462- HostVO lastHost = _hostDao .findById (vm .getLastHostId ());
463- if (lastHost == null ) {
464- logger .debug ("Unable to deploy VM {} in the last host, last host doesn't exist" , vm .getName ());
465- return null ;
466- }
467-
468- logger .debug ("VM's last host is {}, trying to choose the same host if it is not in maintenance, error or degraded state" , lastHost );
469- if (lastHost .isInMaintenanceStates () || Arrays .asList (ResourceState .Error , ResourceState .Degraded ).contains (lastHost .getResourceState ())) {
470- logger .debug ("Unable to deploy VM {} in the last host, last host {} is in {} state" , vm .getName (), lastHost .getName (), lastHost .getResourceState ());
471- return null ;
472- }
473-
474- return lastHost ;
475- }
476-
477450 private void avoidDifferentArchResources (VirtualMachineProfile vmProfile , DataCenter dc , ExcludeList avoids ) {
478451 VirtualMachineTemplate template = vmProfile .getTemplate ();
479452 for (CPU .CPUArch arch : clusterArchTypes ) {
@@ -493,56 +466,65 @@ private void avoidDifferentArchResources(VirtualMachineProfile vmProfile, DataCe
493466 private DeployDestination deployInVmLastHost (VirtualMachineProfile vmProfile , DeploymentPlan plan , ExcludeList avoids ,
494467 DeploymentPlanner planner , VirtualMachine vm , DataCenter dc , ServiceOffering offering , int cpuRequested , long ramRequested ,
495468 boolean volumesRequireEncryption ) throws InsufficientServerCapacityException {
496- HostVO host = _hostDao .findById (vm .getLastHostId ());
497- if (canUseLastHost (host , avoids , plan , vm , offering , volumesRequireEncryption )) {
498- _hostDao .loadHostTags (host );
499- _hostDao .loadDetails (host );
500- if (host .getStatus () != Status .Up ) {
469+ String considerLastHostStr = (String )vmProfile .getParameter (VirtualMachineProfile .Param .ConsiderLastHost );
470+ String haVmTag = (String )vmProfile .getParameter (VirtualMachineProfile .Param .HaTag );
471+ boolean considerLastHost = vm .getLastHostId () != null && haVmTag == null &&
472+ !(Boolean .FALSE .toString ().equalsIgnoreCase (considerLastHostStr ));
473+ if (!considerLastHost ) {
474+ return null ;
475+ }
476+
477+ logger .debug ("This VM has last host_id: {}" , vm .getLastHostId ());
478+ HostVO lastHost = _hostDao .findById (vm .getLastHostId ());
479+ if (canUseLastHost (lastHost , avoids , plan , vm , offering , volumesRequireEncryption )) {
480+ _hostDao .loadHostTags (lastHost );
481+ _hostDao .loadDetails (lastHost );
482+ if (lastHost .getStatus () != Status .Up ) {
501483 logger .debug ("Cannot deploy VM [{}] to the last host [{}] because this host is not in UP state or is not enabled. Host current status [{}] and resource status [{}]." ,
502- vm , host , host .getState ().name (), host .getResourceState ());
484+ vm , lastHost , lastHost .getState ().name (), lastHost .getResourceState ());
503485 return null ;
504486 }
505- if (checkVmProfileAndHost (vmProfile , host )) {
506- long cluster_id = host .getClusterId ();
487+ if (checkVmProfileAndHost (vmProfile , lastHost )) {
488+ long cluster_id = lastHost .getClusterId ();
507489 ClusterDetailsVO cluster_detail_cpu = _clusterDetailsDao .findDetail (cluster_id , "cpuOvercommitRatio" );
508490 ClusterDetailsVO cluster_detail_ram = _clusterDetailsDao .findDetail (cluster_id , "memoryOvercommitRatio" );
509491 float cpuOvercommitRatio = Float .parseFloat (cluster_detail_cpu .getValue ());
510492 float memoryOvercommitRatio = Float .parseFloat (cluster_detail_ram .getValue ());
511493
512494 boolean hostHasCpuCapability , hostHasCapacity = false ;
513- hostHasCpuCapability = _capacityMgr .checkIfHostHasCpuCapability (host , offering .getCpu (), offering .getSpeed ());
495+ hostHasCpuCapability = _capacityMgr .checkIfHostHasCpuCapability (lastHost , offering .getCpu (), offering .getSpeed ());
514496
515497 if (hostHasCpuCapability ) {
516498 // first check from reserved capacity
517- hostHasCapacity = _capacityMgr .checkIfHostHasCapacity (host , cpuRequested , ramRequested , true , cpuOvercommitRatio , memoryOvercommitRatio , true );
499+ hostHasCapacity = _capacityMgr .checkIfHostHasCapacity (lastHost , cpuRequested , ramRequested , true , cpuOvercommitRatio , memoryOvercommitRatio , true );
518500
519501 // if not reserved, check the free capacity
520502 if (!hostHasCapacity )
521- hostHasCapacity = _capacityMgr .checkIfHostHasCapacity (host , cpuRequested , ramRequested , false , cpuOvercommitRatio , memoryOvercommitRatio , true );
503+ hostHasCapacity = _capacityMgr .checkIfHostHasCapacity (lastHost , cpuRequested , ramRequested , false , cpuOvercommitRatio , memoryOvercommitRatio , true );
522504 }
523505
524506 boolean displayStorage = getDisplayStorageFromVmProfile (vmProfile );
525507 if (!hostHasCapacity || !hostHasCpuCapability ) {
526- logger .debug ("Cannot deploy VM [{}] to the last host [{}] because this host does not have enough capacity to deploy this VM." , vm , host );
508+ logger .debug ("Cannot deploy VM [{}] to the last host [{}] because this host does not have enough capacity to deploy this VM." , vm , lastHost );
527509 return null ;
528510 }
529- Pod pod = _podDao .findById (host .getPodId ());
530- Cluster cluster = _clusterDao .findById (host .getClusterId ());
511+ Pod pod = _podDao .findById (lastHost .getPodId ());
512+ Cluster cluster = _clusterDao .findById (lastHost .getClusterId ());
531513
532514 logger .debug ("Last host [{}] of VM [{}] is UP and has enough capacity. Checking for suitable pools for this host under zone [{}], pod [{}] and cluster [{}]." ,
533- host , vm , dc , pod , cluster );
515+ lastHost , vm , dc , pod , cluster );
534516
535517 if (vm .getHypervisorType () == HypervisorType .BareMetal ) {
536- DeployDestination dest = new DeployDestination (dc , pod , cluster , host , new HashMap <>(), displayStorage );
518+ DeployDestination dest = new DeployDestination (dc , pod , cluster , lastHost , new HashMap <>(), displayStorage );
537519 logger .debug ("Returning Deployment Destination: {}." , dest );
538520 return dest ;
539521 }
540522
541523 // search for storage under the zone, pod, cluster
542524 // of
543525 // the last host.
544- DataCenterDeployment lastPlan = new DataCenterDeployment (host .getDataCenterId (),
545- host .getPodId (), host .getClusterId (), host .getId (), plan .getPoolId (), null );
526+ DataCenterDeployment lastPlan = new DataCenterDeployment (lastHost .getDataCenterId (),
527+ lastHost .getPodId (), lastHost .getClusterId (), lastHost .getId (), plan .getPoolId (), null );
546528 Pair <Map <Volume , List <StoragePool >>, List <Volume >> result = findSuitablePoolsForVolumes (
547529 vmProfile , lastPlan , avoids , HostAllocator .RETURN_UPTO_ALL );
548530 Map <Volume , List <StoragePool >> suitableVolumeStoragePools = result .first ();
@@ -551,11 +533,11 @@ private DeployDestination deployInVmLastHost(VirtualMachineProfile vmProfile, De
551533 // choose the potential pool for this VM for this
552534 // host
553535 if (suitableVolumeStoragePools .isEmpty ()) {
554- logger .debug ("Cannot find suitable storage pools in host [{}] to deploy VM [{}]" , host , vm );
536+ logger .debug ("Cannot find suitable storage pools in host [{}] to deploy VM [{}]" , lastHost , vm );
555537 return null ;
556538 }
557539 List <Host > suitableHosts = new ArrayList <>();
558- suitableHosts .add (host );
540+ suitableHosts .add (lastHost );
559541 Pair <Host , Map <Volume , StoragePool >> potentialResources = findPotentialDeploymentResources (
560542 suitableHosts , suitableVolumeStoragePools , avoids ,
561543 getPlannerUsage (planner , vmProfile , plan , avoids ), readyAndReusedVolumes , plan .getPreferredHosts (), vm );
@@ -568,7 +550,7 @@ private DeployDestination deployInVmLastHost(VirtualMachineProfile vmProfile, De
568550 for (Volume vol : readyAndReusedVolumes ) {
569551 storageVolMap .remove (vol );
570552 }
571- DeployDestination dest = new DeployDestination (dc , pod , cluster , host , storageVolMap , displayStorage );
553+ DeployDestination dest = new DeployDestination (dc , pod , cluster , lastHost , storageVolMap , displayStorage );
572554 logger .debug ("Returning Deployment Destination: {}" , dest );
573555 return dest ;
574556 }
@@ -580,7 +562,7 @@ private DeployDestination deployInVmLastHost(VirtualMachineProfile vmProfile, De
580562
581563 private boolean canUseLastHost (HostVO host , ExcludeList avoids , DeploymentPlan plan , VirtualMachine vm , ServiceOffering offering , boolean volumesRequireEncryption ) {
582564 if (host == null ) {
583- logger .warn ("Could not find last host of VM [{}] with id [{}]. Skipping this and trying other available hosts. " , vm , vm .getLastHostId ());
565+ logger .warn ("Could not find last host of VM [{}] with id [{}]. Skipping it " , vm , vm .getLastHostId ());
584566 return false ;
585567 }
586568
@@ -594,6 +576,12 @@ private boolean canUseLastHost(HostVO host, ExcludeList avoids, DeploymentPlan p
594576 return false ;
595577 }
596578
579+ logger .debug ("VM's last host is {}, trying to choose the same host if it is not in maintenance, error or degraded state" , host );
580+ if (host .isInMaintenanceStates () || Arrays .asList (ResourceState .Error , ResourceState .Degraded ).contains (host .getResourceState ())) {
581+ logger .debug ("Unable to deploy VM {} in the last host, last host {} is in {} state" , vm .getName (), host .getName (), host .getResourceState ());
582+ return false ;
583+ }
584+
597585 if (_capacityMgr .checkIfHostReachMaxGuestLimit (host )) {
598586 logger .debug ("Cannot deploy VM [{}] in the last host [{}] because this host already has the max number of running VMs (users and system VMs). Skipping this and trying other available hosts." ,
599587 vm , host );
0 commit comments