55
55
import com .cloud .configuration .ConfigurationManager ;
56
56
import com .cloud .dc .ClusterDetailsDao ;
57
57
import com .cloud .dc .ClusterDetailsVO ;
58
- import com .cloud .dc .ClusterVO ;
59
58
import com .cloud .dc .dao .ClusterDao ;
60
59
import com .cloud .deploy .DeploymentClusterPlanner ;
61
60
import com .cloud .event .UsageEventVO ;
@@ -277,6 +276,9 @@ public void doInTransactionWithoutResult(TransactionStatus status) {
277
276
@ Override
278
277
public void allocateVmCapacity (VirtualMachine vm , final boolean fromLastHost ) {
279
278
279
+ if (vm == null ) {
280
+ return ;
281
+ }
280
282
final long hostId = vm .getHostId ();
281
283
HostVO host = _hostDao .findById (hostId );
282
284
final long clusterId = host .getClusterId ();
@@ -287,18 +289,22 @@ public void allocateVmCapacity(VirtualMachine vm, final boolean fromLastHost) {
287
289
288
290
CapacityVO capacityCpu = _capacityDao .findByHostIdType (hostId , Capacity .CAPACITY_TYPE_CPU );
289
291
CapacityVO capacityMem = _capacityDao .findByHostIdType (hostId , Capacity .CAPACITY_TYPE_MEMORY );
290
-
291
292
if (capacityCpu == null || capacityMem == null || svo == null ) {
292
293
return ;
293
294
}
294
295
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 ) ;
297
298
298
299
try {
299
300
final long capacityCpuId = capacityCpu .getId ();
300
301
final long capacityMemId = capacityMem .getId ();
301
302
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
+
302
308
Transaction .execute (new TransactionCallbackNoReturn () {
303
309
@ Override
304
310
public void doInTransactionWithoutResult (TransactionStatus status ) {
@@ -602,33 +608,18 @@ public void updateCapacityForHost(final Host host) {
602
608
s_logger .debug ("Found " + vms .size () + " VMs on host " + host .getId ());
603
609
}
604
610
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
+
612
612
for (VMInstanceVO vm : vms ) {
613
613
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
- }
621
614
ServiceOffering so = offeringsMap .get (vm .getServiceOfferingId ());
622
615
if (so .isDynamic ()) {
623
616
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 );
626
618
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 ())));
629
620
} 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 ());
632
623
}
633
624
}
634
625
@@ -639,25 +630,16 @@ public void updateCapacityForHost(final Host host) {
639
630
for (VMInstanceVO vm : vmsByLastHostId ) {
640
631
long secondsSinceLastUpdate = (DateUtil .currentGMTTime ().getTime () - vm .getUpdateTime ().getTime ()) / 1000 ;
641
632
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
- }
649
633
ServiceOffering so = offeringsMap .get (vm .getServiceOfferingId ());
650
634
Map <String , String > vmDetails = _userVmDetailsDao .listDetailsKeyPairs (vm .getId ());
651
635
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 ())));
658
640
} 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 ()) ;
661
643
}
662
644
} else {
663
645
// 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
843
825
allocateVmCapacity (vm , fromLastHost );
844
826
}
845
827
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
+
846
837
if (newState == State .Stopped ) {
847
838
if (vm .getType () == VirtualMachine .Type .User ) {
848
839
0 commit comments