@@ -2646,6 +2646,9 @@ public boolean processCommands(long agentId, long seq, Command[] cmds) {
26462646*/
26472647
26482648 }
2649+
2650+ // take the chance to scan stalled VM
2651+ scanStalledVMInTransitionState (agentId );
26492652 processed = true ;
26502653 }
26512654 }
@@ -3411,6 +3414,10 @@ public boolean reConfigureVm(VirtualMachine vm, ServiceOffering oldServiceOfferi
34113414
34123415 }
34133416
3417+ //
3418+ // PowerState report handling for out-of-band changes and handling of left-over transitional VM states
3419+ //
3420+
34143421 @ MessageHandler (topic =TopicConstants .VM_POWER_STATE )
34153422 private void HandlePownerStateReport (Object target , String subject , String senderAddress , Object args ) {
34163423 assert (args != null );
@@ -3447,33 +3454,60 @@ private void HandlePownerStateReport(Object target, String subject, String sende
34473454 }
34483455
34493456 private void HandlePowerOnReportWithNoPendingJobsOnVM (VMInstanceVO vm ) {
3450-
3451- // TODO :
3457+ //
34523458 // 1) handle left-over transitional VM states
34533459 // 2) handle out of band VM live migration
34543460 // 3) handle out of sync stationary states, marking VM from Stopped to Running with
34553461 // alert messages
3456-
3462+ //
34573463 switch (vm .getState ()) {
3458- case Starting :
3464+ case Starting :
3465+ try {
3466+ stateTransitTo (vm , VirtualMachine .Event .FollowAgentPowerOnReport , vm .getPowerHostId ());
3467+ } catch (NoTransitionException e ) {
3468+ s_logger .warn ("Unexpected VM state transition exception, race-condition?" , e );
3469+ }
3470+ // TODO we need to alert admin or user about this risky state transition
34593471 break ;
34603472
34613473 case Running :
3474+ try {
3475+ if (vm .getHostId () != null && vm .getHostId ().longValue () != vm .getPowerHostId ().longValue ())
3476+ s_logger .info ("Detected out of band VM migration from host " + vm .getHostId () + " to host " + vm .getPowerHostId ());
3477+ stateTransitTo (vm , VirtualMachine .Event .FollowAgentPowerOnReport , vm .getPowerHostId ());
3478+ } catch (NoTransitionException e ) {
3479+ s_logger .warn ("Unexpected VM state transition exception, race-condition?" , e );
3480+ }
34623481 break ;
34633482
34643483 case Stopping :
34653484 case Stopped :
3485+ try {
3486+ stateTransitTo (vm , VirtualMachine .Event .FollowAgentPowerOnReport , vm .getPowerHostId ());
3487+ } catch (NoTransitionException e ) {
3488+ s_logger .warn ("Unexpected VM state transition exception, race-condition?" , e );
3489+ }
3490+ // TODO we need to alert admin or user about this risky state transition
34663491 break ;
34673492
34683493 case Destroyed :
34693494 case Expunging :
3495+ s_logger .info ("Receive power on report when VM is in destroyed or expunging state. vm: "
3496+ + vm .getId () + ", state: " + vm .getState ());
34703497 break ;
34713498
34723499 case Migrating :
3500+ try {
3501+ stateTransitTo (vm , VirtualMachine .Event .FollowAgentPowerOnReport , vm .getPowerHostId ());
3502+ } catch (NoTransitionException e ) {
3503+ s_logger .warn ("Unexpected VM state transition exception, race-condition?" , e );
3504+ }
34733505 break ;
34743506
34753507 case Error :
34763508 default :
3509+ s_logger .info ("Receive power on report when VM is in error or unexpected state. vm: "
3510+ + vm .getId () + ", state: " + vm .getState ());
34773511 break ;
34783512 }
34793513 }
@@ -3485,13 +3519,15 @@ private void HandlePowerOffReportWithNoPendingJobsOnVM(VMInstanceVO vm) {
34853519 // 2) handle out of sync stationary states, schedule force-stop to release resources
34863520 //
34873521 switch (vm .getState ()) {
3488- case Starting :
3522+ case Starting :
34893523 break ;
34903524
34913525 case Running :
34923526 break ;
34933527
34943528 case Stopping :
3529+ break ;
3530+
34953531 case Stopped :
34963532 break ;
34973533
@@ -3507,4 +3543,18 @@ private void HandlePowerOffReportWithNoPendingJobsOnVM(VMInstanceVO vm) {
35073543 break ;
35083544 }
35093545 }
3546+
3547+ private void scanStalledVMInTransitionState (long hostId ) {
3548+ //
3549+ // TODO check VM that is stuck in Starting, Stopping, Migrating states, we won't check
3550+ // VMs in expunging state (this need to be handled specially)
3551+ //
3552+ // checking condition
3553+ // 1) no pending VmWork job
3554+ // 2) no power state update for some time
3555+ // 3) on hostId host
3556+
3557+
3558+
3559+ }
35103560}
0 commit comments