@@ -906,8 +906,10 @@ public Boolean doInTransaction(TransactionStatus status) {
906906 && (null == instance .getPowerStateUpdateTime ()
907907 || instance .getPowerStateUpdateTime ().before (wisdomEra ))) {
908908 Long savedPowerHostId = instance .getPowerHostId ();
909- if (instance .getPowerState () != powerState || savedPowerHostId == null
910- || savedPowerHostId != powerHostId ) {
909+ if (instance .getPowerState () != powerState
910+ || savedPowerHostId == null
911+ || savedPowerHostId != powerHostId
912+ || !isPowerStateInSyncWithInstanceState (powerState , powerHostId , instance )) {
911913 instance .setPowerState (powerState );
912914 instance .setPowerHostId (powerHostId );
913915 instance .setPowerStateUpdateCount (1 );
@@ -921,31 +923,6 @@ public Boolean doInTransaction(TransactionStatus status) {
921923 instance .setPowerStateUpdateTime (DateUtil .currentGMTTime ());
922924 needToUpdate = true ;
923925 update (instanceId , instance );
924- } else {
925- switch (instance .getState ()) {
926- case Starting :
927- case Running :
928- if (powerState == VirtualMachine .PowerState .PowerOff ) {
929- s_logger .debug (String .format ("VM id: %d on host id: %d and power host id: %d is in %s state, but power state is %s" ,
930- instanceId , instance .getHostId (), powerHostId , instance .getState (), powerState ));
931- instance .setPowerStateUpdateTime (DateUtil .currentGMTTime ());
932- instance .setPowerStateUpdateCount (1 );
933- needToUpdate = true ;
934- update (instanceId , instance );
935- }
936- break ;
937- case Stopping :
938- case Stopped :
939- if (powerState == VirtualMachine .PowerState .PowerOn ) {
940- s_logger .debug (String .format ("VM id: %d on host id: %d and power host id: %d is in %s state, but power state is %s" ,
941- instanceId , instance .getHostId (), powerHostId , instance .getState (), powerState ));
942- instance .setPowerStateUpdateTime (DateUtil .currentGMTTime ());
943- instance .setPowerStateUpdateCount (1 );
944- needToUpdate = true ;
945- update (instanceId , instance );
946- }
947- break ;
948- }
949926 }
950927 }
951928 }
@@ -954,6 +931,28 @@ public Boolean doInTransaction(TransactionStatus status) {
954931 });
955932 }
956933
934+ private boolean isPowerStateInSyncWithInstanceState (final VirtualMachine .PowerState powerState , final long powerHostId , final VMInstanceVO instance ) {
935+ switch (instance .getState ()) {
936+ case Starting :
937+ case Running :
938+ if (powerState == VirtualMachine .PowerState .PowerOff ) {
939+ s_logger .debug (String .format ("VM id: %d on host id: %d and power host id: %d is in %s state, but power state is %s" ,
940+ instance .getId (), instance .getHostId (), powerHostId , instance .getState (), powerState ));
941+ return false ;
942+ }
943+ break ;
944+ case Stopping :
945+ case Stopped :
946+ if (powerState == VirtualMachine .PowerState .PowerOn ) {
947+ s_logger .debug (String .format ("VM id: %d on host id: %d and power host id: %d is in %s state, but power state is %s" ,
948+ instance .getId (), instance .getHostId (), powerHostId , instance .getState (), powerState ));
949+ return false ;
950+ }
951+ break ;
952+ }
953+ return true ;
954+ }
955+
957956 @ Override
958957 public boolean isPowerStateUpToDate (final long instanceId ) {
959958 VMInstanceVO instance = findById (instanceId );
0 commit comments