4848import com .cloud .configuration .Config ;
4949import com .cloud .dc .ClusterDetailsDao ;
5050import com .cloud .dc .ClusterDetailsVO ;
51- import com .cloud .dc .ClusterVO ;
5251import com .cloud .dc .dao .ClusterDao ;
5352import com .cloud .deploy .DeploymentClusterPlanner ;
5453import com .cloud .event .UsageEventVO ;
@@ -264,6 +263,9 @@ public void doInTransactionWithoutResult(TransactionStatus status) {
264263 @ Override
265264 public void allocateVmCapacity (VirtualMachine vm , final boolean fromLastHost ) {
266265
266+ if (vm == null ) {
267+ return ;
268+ }
267269 final long hostId = vm .getHostId ();
268270 HostVO host = _hostDao .findById (hostId );
269271 final long clusterId = host .getClusterId ();
@@ -274,18 +276,22 @@ public void allocateVmCapacity(VirtualMachine vm, final boolean fromLastHost) {
274276
275277 CapacityVO capacityCpu = _capacityDao .findByHostIdType (hostId , Capacity .CAPACITY_TYPE_CPU );
276278 CapacityVO capacityMem = _capacityDao .findByHostIdType (hostId , Capacity .CAPACITY_TYPE_MEMORY );
277-
278279 if (capacityCpu == null || capacityMem == null || svo == null ) {
279280 return ;
280281 }
281282
282- final int cpu = svo .getCpu () * svo .getSpeed ();
283- final long ram = svo .getRamSize () * 1024L * 1024L ;
283+ final int cpu = ( int ) ( svo .getCpu () * svo .getSpeed () );
284+ final long ram = ( long ) ( svo .getRamSize () * 1024L * 1024L ) ;
284285
285286 try {
286287 final long capacityCpuId = capacityCpu .getId ();
287288 final long capacityMemId = capacityMem .getId ();
288289
290+ // 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.
291+ VMInstanceVO vmInstanceVO = _vmDao .findById (vm .getId ());
292+ _userVmDetailsDao .addDetail (vmInstanceVO .getId (), "cpuOvercommitRatio" , String .valueOf (cpuOvercommitRatio ), true );
293+ _userVmDetailsDao .addDetail (vmInstanceVO .getId (), "memoryOvercommitRatio" , String .valueOf (memoryOvercommitRatio ), true );
294+
289295 Transaction .execute (new TransactionCallbackNoReturn () {
290296 @ Override
291297 public void doInTransactionWithoutResult (TransactionStatus status ) {
@@ -591,35 +597,19 @@ public void updateCapacityForHost(final Host host) {
591597 s_logger .debug ("Found " + vms .size () + " VMs on host " + host .getId ());
592598 }
593599
594- ClusterVO cluster = _clusterDao .findById (host .getClusterId ());
595- ClusterDetailsVO clusterDetailCpu = _clusterDetailsDao .findDetail (cluster .getId (), "cpuOvercommitRatio" );
596- ClusterDetailsVO clusterDetailRam = _clusterDetailsDao .findDetail (cluster .getId (), "memoryOvercommitRatio" );
597- Float clusterCpuOvercommitRatio = Float .parseFloat (clusterDetailCpu .getValue ());
598- Float clusterRamOvercommitRatio = Float .parseFloat (clusterDetailRam .getValue ());
599600 for (VMInstanceVO vm : vms ) {
600601 Float cpuOvercommitRatio = 1.0f ;
601602 Float ramOvercommitRatio = 1.0f ;
602603 Map <String , String > vmDetails = _userVmDetailsDao .listDetailsKeyPairs (vm .getId ());
603- String vmDetailCpu = vmDetails .get ("cpuOvercommitRatio" );
604- String vmDetailRam = vmDetails .get ("memoryOvercommitRatio" );
605- if (vmDetailCpu != null ) {
606- //if vmDetail_cpu is not null it means it is running in a overcommited cluster.
607- cpuOvercommitRatio = Float .parseFloat (vmDetailCpu );
608- ramOvercommitRatio = Float .parseFloat (vmDetailRam );
609- }
610604 ServiceOffering so = offeringsMap .get (vm .getServiceOfferingId ());
611605 if (so .isDynamic ()) {
612606 usedMemory +=
613- ((Integer .parseInt (vmDetails .get (UsageEventVO .DynamicParameters .memory .name ())) * 1024L * 1024L ) / ramOvercommitRatio ) *
614- clusterRamOvercommitRatio ;
607+ (Integer .parseInt (vmDetails .get (UsageEventVO .DynamicParameters .memory .name ())) * 1024L * 1024L );
615608 usedCpu +=
616- ((Integer .parseInt (vmDetails .get (UsageEventVO .DynamicParameters .cpuNumber .name ())) * Integer .parseInt (vmDetails .get (UsageEventVO .DynamicParameters .cpuSpeed .name ()))) / cpuOvercommitRatio ) *
617- clusterCpuOvercommitRatio ;
618- usedCpuCore += Integer .parseInt (vmDetails .get (UsageEventVO .DynamicParameters .cpuNumber .name ()));
609+ (Integer .parseInt (vmDetails .get (UsageEventVO .DynamicParameters .cpuNumber .name ())) * Integer .parseInt (vmDetails .get (UsageEventVO .DynamicParameters .cpuSpeed .name ())));
619610 } else {
620- usedMemory += ((so .getRamSize () * 1024L * 1024L ) / ramOvercommitRatio ) * clusterRamOvercommitRatio ;
621- usedCpu += ((so .getCpu () * so .getSpeed ()) / cpuOvercommitRatio ) * clusterCpuOvercommitRatio ;
622- usedCpuCore += so .getCpu ();
611+ usedMemory += (so .getRamSize () * 1024L * 1024L );
612+ usedCpu += (so .getCpu () * so .getSpeed ());
623613 }
624614 }
625615
@@ -632,27 +622,16 @@ public void updateCapacityForHost(final Host host) {
632622 Float ramOvercommitRatio = 1.0f ;
633623 long secondsSinceLastUpdate = (DateUtil .currentGMTTime ().getTime () - vm .getUpdateTime ().getTime ()) / 1000 ;
634624 if (secondsSinceLastUpdate < _vmCapacityReleaseInterval ) {
635- UserVmDetailVO vmDetailCpu = _userVmDetailsDao .findDetail (vm .getId (), "cpuOvercommitRatio" );
636- UserVmDetailVO vmDetailRam = _userVmDetailsDao .findDetail (vm .getId (), "memoryOvercommitRatio" );
637- if (vmDetailCpu != null ) {
638- //if vmDetail_cpu is not null it means it is running in a overcommited cluster.
639- cpuOvercommitRatio = Float .parseFloat (vmDetailCpu .getValue ());
640- ramOvercommitRatio = Float .parseFloat (vmDetailRam .getValue ());
641- }
642625 ServiceOffering so = offeringsMap .get (vm .getServiceOfferingId ());
643626 Map <String , String > vmDetails = _userVmDetailsDao .listDetailsKeyPairs (vm .getId ());
644627 if (so .isDynamic ()) {
645- reservedMemory +=
646- ((Integer .parseInt (vmDetails .get (UsageEventVO .DynamicParameters .memory .name ())) * 1024L * 1024L ) / ramOvercommitRatio ) *
647- clusterRamOvercommitRatio ;
648- reservedCpu +=
649- ((Integer .parseInt (vmDetails .get (UsageEventVO .DynamicParameters .cpuNumber .name ())) * Integer .parseInt (vmDetails .get (UsageEventVO .DynamicParameters .cpuSpeed .name ()))) / cpuOvercommitRatio ) *
650- clusterCpuOvercommitRatio ;
651- reservedCpuCore += Integer .parseInt (vmDetails .get (UsageEventVO .DynamicParameters .cpuNumber .name ()));
628+ reservedMemory +=
629+ ((Integer .parseInt (vmDetails .get (UsageEventVO .DynamicParameters .memory .name ())) * 1024L * 1024L ));
630+ reservedCpu +=
631+ (Integer .parseInt (vmDetails .get (UsageEventVO .DynamicParameters .cpuNumber .name ())) * Integer .parseInt (vmDetails .get (UsageEventVO .DynamicParameters .cpuSpeed .name ())));
652632 } else {
653- reservedMemory += ((so .getRamSize () * 1024L * 1024L ) / ramOvercommitRatio ) * clusterRamOvercommitRatio ;
654- reservedCpu += (so .getCpu () * so .getSpeed () / cpuOvercommitRatio ) * clusterCpuOvercommitRatio ;
655- reservedCpuCore += so .getCpu ();
633+ reservedMemory += (so .getRamSize () * 1024L * 1024L );
634+ reservedCpu += (so .getCpu () * so .getSpeed ());
656635 }
657636 } else {
658637 // signal if not done already, that the VM has been stopped for skip.counting.hours,
@@ -885,6 +864,15 @@ public boolean postStateTransitionEvent(StateMachine2.Transition<State, Event> t
885864 allocateVmCapacity (vm , fromLastHost );
886865 }
887866
867+ if (oldState == State .Migrating && newState == State .Running ) {
868+ boolean fromLastHost = false ;
869+ if (vm .getHostId ().equals (vm .getLastHostId ())) {
870+ s_logger .debug ("VM starting again on the last host it was stopped on" );
871+ fromLastHost = true ;
872+ }
873+ allocateVmCapacity (vm , fromLastHost );
874+ }
875+
888876 if (newState == State .Stopped ) {
889877 if (vm .getType () == VirtualMachine .Type .User ) {
890878
0 commit comments