5555import com .cloud .configuration .ConfigurationManager ;
5656import com .cloud .dc .ClusterDetailsDao ;
5757import com .cloud .dc .ClusterDetailsVO ;
58- import com .cloud .dc .ClusterVO ;
5958import com .cloud .dc .dao .ClusterDao ;
6059import com .cloud .deploy .DeploymentClusterPlanner ;
6160import com .cloud .event .UsageEventVO ;
@@ -277,6 +276,9 @@ public void doInTransactionWithoutResult(TransactionStatus status) {
277276 @ Override
278277 public void allocateVmCapacity (VirtualMachine vm , final boolean fromLastHost ) {
279278
279+ if (vm == null ) {
280+ return ;
281+ }
280282 final long hostId = vm .getHostId ();
281283 HostVO host = _hostDao .findById (hostId );
282284 final long clusterId = host .getClusterId ();
@@ -287,18 +289,22 @@ public void allocateVmCapacity(VirtualMachine vm, final boolean fromLastHost) {
287289
288290 CapacityVO capacityCpu = _capacityDao .findByHostIdType (hostId , Capacity .CAPACITY_TYPE_CPU );
289291 CapacityVO capacityMem = _capacityDao .findByHostIdType (hostId , Capacity .CAPACITY_TYPE_MEMORY );
290-
291292 if (capacityCpu == null || capacityMem == null || svo == null ) {
292293 return ;
293294 }
294295
295- final int cpu = svo .getCpu () * svo .getSpeed ();
296- final long ram = svo .getRamSize () * 1024L * 1024L ;
296+ final int cpu = ( int ) ( svo .getCpu () * svo .getSpeed () );
297+ final long ram = ( long ) ( svo .getRamSize () * 1024L * 1024L ) ;
297298
298299 try {
299300 final long capacityCpuId = capacityCpu .getId ();
300301 final long capacityMemId = capacityMem .getId ();
301302
303+ // Update the over commit ratio of the VM to reflect the same value as that of the cluster to which it has been migrated to / deployed on.
304+ VMInstanceVO vmInstanceVO = _vmDao .findById (vm .getId ());
305+ _userVmDetailsDao .addDetail (vmInstanceVO .getId (), "cpuOvercommitRatio" , String .valueOf (cpuOvercommitRatio ), true );
306+ _userVmDetailsDao .addDetail (vmInstanceVO .getId (), "memoryOvercommitRatio" , String .valueOf (memoryOvercommitRatio ), true );
307+
302308 Transaction .execute (new TransactionCallbackNoReturn () {
303309 @ Override
304310 public void doInTransactionWithoutResult (TransactionStatus status ) {
@@ -602,33 +608,18 @@ public void updateCapacityForHost(final Host host) {
602608 s_logger .debug ("Found " + vms .size () + " VMs on host " + host .getId ());
603609 }
604610
605- ClusterVO cluster = _clusterDao .findById (host .getClusterId ());
606- ClusterDetailsVO clusterDetailCpu = _clusterDetailsDao .findDetail (cluster .getId (), "cpuOvercommitRatio" );
607- ClusterDetailsVO clusterDetailRam = _clusterDetailsDao .findDetail (cluster .getId (), "memoryOvercommitRatio" );
608- Float clusterCpuOvercommitRatio = Float .parseFloat (clusterDetailCpu .getValue ());
609- Float clusterRamOvercommitRatio = Float .parseFloat (clusterDetailRam .getValue ());
610- Float cpuOvercommitRatio = 1f ;
611- Float ramOvercommitRatio = 1f ;
611+
612612 for (VMInstanceVO vm : vms ) {
613613 Map <String , String > vmDetails = _userVmDetailsDao .listDetailsKeyPairs (vm .getId ());
614- String vmDetailCpu = vmDetails .get ("cpuOvercommitRatio" );
615- String vmDetailRam = vmDetails .get ("memoryOvercommitRatio" );
616- if (vmDetailCpu != null ) {
617- //if vmDetail_cpu is not null it means it is running in a overcommited cluster.
618- cpuOvercommitRatio = Float .parseFloat (vmDetailCpu );
619- ramOvercommitRatio = Float .parseFloat (vmDetailRam );
620- }
621614 ServiceOffering so = offeringsMap .get (vm .getServiceOfferingId ());
622615 if (so .isDynamic ()) {
623616 usedMemory +=
624- ((Integer .parseInt (vmDetails .get (UsageEventVO .DynamicParameters .memory .name ())) * 1024L * 1024L ) / ramOvercommitRatio ) *
625- clusterRamOvercommitRatio ;
617+ (Integer .parseInt (vmDetails .get (UsageEventVO .DynamicParameters .memory .name ())) * 1024L * 1024L );
626618 usedCpu +=
627- ((Integer .parseInt (vmDetails .get (UsageEventVO .DynamicParameters .cpuNumber .name ())) * Integer .parseInt (vmDetails .get (UsageEventVO .DynamicParameters .cpuSpeed .name ()))) / cpuOvercommitRatio ) *
628- clusterCpuOvercommitRatio ;
619+ (Integer .parseInt (vmDetails .get (UsageEventVO .DynamicParameters .cpuNumber .name ())) * Integer .parseInt (vmDetails .get (UsageEventVO .DynamicParameters .cpuSpeed .name ())));
629620 } else {
630- usedMemory += (( so .getRamSize () * 1024L * 1024L ) / ramOvercommitRatio ) * clusterRamOvercommitRatio ;
631- usedCpu += (( so .getCpu () * so .getSpeed ()) / cpuOvercommitRatio ) * clusterCpuOvercommitRatio ;
621+ usedMemory += (so .getRamSize () * 1024L * 1024L );
622+ usedCpu += (so .getCpu () * so .getSpeed ());
632623 }
633624 }
634625
@@ -639,25 +630,16 @@ public void updateCapacityForHost(final Host host) {
639630 for (VMInstanceVO vm : vmsByLastHostId ) {
640631 long secondsSinceLastUpdate = (DateUtil .currentGMTTime ().getTime () - vm .getUpdateTime ().getTime ()) / 1000 ;
641632 if (secondsSinceLastUpdate < _vmCapacityReleaseInterval ) {
642- UserVmDetailVO vmDetailCpu = _userVmDetailsDao .findDetail (vm .getId (), "cpuOvercommitRatio" );
643- UserVmDetailVO vmDetailRam = _userVmDetailsDao .findDetail (vm .getId (), "memoryOvercommitRatio" );
644- if (vmDetailCpu != null ) {
645- //if vmDetail_cpu is not null it means it is running in a overcommited cluster.
646- cpuOvercommitRatio = Float .parseFloat (vmDetailCpu .getValue ());
647- ramOvercommitRatio = Float .parseFloat (vmDetailRam .getValue ());
648- }
649633 ServiceOffering so = offeringsMap .get (vm .getServiceOfferingId ());
650634 Map <String , String > vmDetails = _userVmDetailsDao .listDetailsKeyPairs (vm .getId ());
651635 if (so .isDynamic ()) {
652- reservedMemory +=
653- ((Integer .parseInt (vmDetails .get (UsageEventVO .DynamicParameters .memory .name ())) * 1024L * 1024L ) / ramOvercommitRatio ) *
654- clusterRamOvercommitRatio ;
655- reservedCpu +=
656- ((Integer .parseInt (vmDetails .get (UsageEventVO .DynamicParameters .cpuNumber .name ())) * Integer .parseInt (vmDetails .get (UsageEventVO .DynamicParameters .cpuSpeed .name ()))) / cpuOvercommitRatio ) *
657- clusterCpuOvercommitRatio ;
636+ reservedMemory +=
637+ ((Integer .parseInt (vmDetails .get (UsageEventVO .DynamicParameters .memory .name ())) * 1024L * 1024L ));
638+ reservedCpu +=
639+ (Integer .parseInt (vmDetails .get (UsageEventVO .DynamicParameters .cpuNumber .name ())) * Integer .parseInt (vmDetails .get (UsageEventVO .DynamicParameters .cpuSpeed .name ())));
658640 } else {
659- reservedMemory += (( so .getRamSize () * 1024L * 1024L ) / ramOvercommitRatio ) * clusterRamOvercommitRatio ;
660- reservedCpu += (so .getCpu () * so .getSpeed () / cpuOvercommitRatio ) * clusterCpuOvercommitRatio ;
641+ reservedMemory += (so .getRamSize () * 1024L * 1024L );
642+ reservedCpu += (so .getCpu () * so .getSpeed ()) ;
661643 }
662644 } else {
663645 // signal if not done already, that the VM has been stopped for skip.counting.hours,
@@ -843,6 +825,15 @@ public boolean postStateTransitionEvent(StateMachine2.Transition<State, Event> t
843825 allocateVmCapacity (vm , fromLastHost );
844826 }
845827
828+ if (oldState == State .Migrating && newState == State .Running ) {
829+ boolean fromLastHost = false ;
830+ if (vm .getHostId ().equals (vm .getLastHostId ())) {
831+ s_logger .debug ("VM starting again on the last host it was stopped on" );
832+ fromLastHost = true ;
833+ }
834+ allocateVmCapacity (vm , fromLastHost );
835+ }
836+
846837 if (newState == State .Stopped ) {
847838 if (vm .getType () == VirtualMachine .Type .User ) {
848839
0 commit comments