@@ -1480,14 +1480,12 @@ protected Map<Long, AgentVmInfo> convertToInfos(final Map<String, State> states)
14801480 * something should be cleaned up
14811481 *
14821482 */
1483- protected Command compareState (long hostId , VMInstanceVO vm , final AgentVmInfo info , final boolean fullSync , boolean nativeHA ) {
1483+ protected Command compareState (long hostId , VMInstanceVO vm , final AgentVmInfo info , final boolean fullSync , boolean trackExternalChange ) {
14841484 State agentState = info .state ;
14851485 final String agentName = info .name ;
14861486 final State serverState = vm .getState ();
14871487 final String serverName = vm .getInstanceName ();
14881488
1489- VirtualMachineGuru <VMInstanceVO > vmGuru = getVmGuru (vm );
1490-
14911489 Command command = null ;
14921490
14931491 if (s_logger .isDebugEnabled ()) {
@@ -1512,6 +1510,16 @@ protected Command compareState(long hostId, VMInstanceVO vm, final AgentVmInfo i
15121510 _alertMgr .sendAlert (alertType , vm .getDataCenterIdToDeployIn (), vm .getPodIdToDeployIn (), "VM (name: " + vm .getInstanceName () + ", id: " + vm .getId () + ") stopped on host " + hostDesc + " due to storage failure" ,
15131511 "Virtual Machine " + vm .getInstanceName () + " (id: " + vm .getId () + ") running on host [" + vm .getHostId () + "] stopped due to storage failure." );
15141512 }
1513+
1514+ if (trackExternalChange ) {
1515+ if (hostId != vm .getHostId ()) {
1516+ try {
1517+ stateTransitTo (vm , VirtualMachine .Event .AgentReportMigrated , hostId );
1518+ } catch (NoTransitionException e ) {
1519+ s_logger .warn (e .getMessage ());
1520+ }
1521+ }
1522+ }
15151523
15161524 // if (serverState == State.Migrating) {
15171525 // s_logger.debug("Skipping vm in migrating state: " + vm);
@@ -1525,17 +1533,14 @@ protected Command compareState(long hostId, VMInstanceVO vm, final AgentVmInfo i
15251533 assert (agentState == State .Stopped || agentState == State .Running ) : "If the states we send up is changed, this must be changed." ;
15261534 if (agentState == State .Running ) {
15271535 try {
1528- if (nativeHA ) {
1529- stateTransitTo (vm , VirtualMachine .Event .AgentReportRunning , hostId );
1530- } else {
1531- stateTransitTo (vm , VirtualMachine .Event .AgentReportRunning , vm .getHostId ());
1532- }
1536+ stateTransitTo (vm , VirtualMachine .Event .AgentReportRunning , hostId );
15331537 } catch (NoTransitionException e ) {
15341538 s_logger .warn (e .getMessage ());
15351539 }
15361540 // FIXME: What if someone comes in and sets it to stopping? Then what?
15371541 return null ;
15381542 }
1543+
15391544 s_logger .debug ("State matches but the agent said stopped so let's send a cleanup command anyways." );
15401545 return cleanup (agentName );
15411546 }
@@ -1567,7 +1572,7 @@ protected Command compareState(long hostId, VMInstanceVO vm, final AgentVmInfo i
15671572 // Our records showed that it should be running so let's restart it.
15681573 _haMgr .scheduleRestart (vm , false );
15691574 } else if (serverState == State .Stopping ) {
1570- _haMgr .scheduleStop (vm , vm . getHostId () , WorkType .ForceStop );
1575+ _haMgr .scheduleStop (vm , hostId , WorkType .ForceStop );
15711576 s_logger .debug ("Scheduling a check stop for VM in stopping mode: " + vm );
15721577 } else if (serverState == State .Starting ) {
15731578 s_logger .debug ("Ignoring VM in starting mode: " + vm .getInstanceName ());
@@ -1577,66 +1582,68 @@ protected Command compareState(long hostId, VMInstanceVO vm, final AgentVmInfo i
15771582 } else if (agentState == State .Running ) {
15781583 if (serverState == State .Starting ) {
15791584 if (fullSync ) {
1580- s_logger .debug ("VM state is starting on full sync so updating it to running" );
1581- vm = findById (vm .getType (), vm .getId ());
1582- try {
1583- stateTransitTo (vm , Event .AgentReportRunning , vm .getHostId ());
1584- } catch (NoTransitionException e1 ) {
1585- s_logger .warn (e1 .getMessage ());
1586- }
1587- s_logger .debug ("VM's " + vm + " state is starting on full sync so updating it to Running" );
1588- vm = vmGuru .findById (vm .getId ());
1589-
1590- VirtualMachineProfile <VMInstanceVO > profile = new VirtualMachineProfileImpl <VMInstanceVO >(vm );
1591- List <NicVO > nics = _nicsDao .listByVmId (profile .getId ());
1592- for (NicVO nic : nics ) {
1593- Network network = _networkMgr .getNetwork (nic .getNetworkId ());
1594- NicProfile nicProfile = new NicProfile (nic , network , nic .getBroadcastUri (), nic .getIsolationUri (), null );
1595- profile .addNic (nicProfile );
1596- }
1597-
1598- Commands cmds = new Commands (OnError .Stop );
1599- s_logger .debug ("Finalizing commands that need to be send to complete Start process for the vm " + vm );
1600-
1601- if (vmGuru .finalizeCommandsOnStart (cmds , profile )) {
1602- if (cmds .size () != 0 ) {
1603- try {
1604- _agentMgr .send (vm .getHostId (), cmds );
1605- } catch (OperationTimedoutException e ) {
1606- s_logger .error ("Exception during update for running vm: " + vm , e );
1607- return null ;
1608- } catch (ResourceUnavailableException e ) {
1609- s_logger .error ("Exception during update for running vm: " + vm , e );
1610- return null ;
1611- }
1612- }
1613-
1614- if (vmGuru .finalizeStart (profile , vm .getHostId (), cmds , null )) {
1615- try {
1616- stateTransitTo (vm , Event .AgentReportRunning , vm .getHostId ());
1617- } catch (NoTransitionException e ) {
1618- s_logger .warn (e .getMessage ());
1619- }
1620- } else {
1621- s_logger .error ("Exception during update for running vm: " + vm );
1622- return null ;
1623- }
1624- } else {
1625- s_logger .error ("Unable to finalize commands on start for vm: " + vm );
1585+ try {
1586+ ensureVmRunningContext (hostId , vm , Event .AgentReportRunning );
1587+ } catch (OperationTimedoutException e ) {
1588+ s_logger .error ("Exception during update for running vm: " + vm , e );
16261589 return null ;
1590+ } catch (ResourceUnavailableException e ) {
1591+ s_logger .error ("Exception during update for running vm: " + vm , e );
1592+ return null ;
1593+ } catch (NoTransitionException e ) {
1594+ s_logger .warn (e .getMessage ());
16271595 }
1628-
16291596 }
16301597 } else if (serverState == State .Stopping ) {
16311598 s_logger .debug ("Scheduling a stop command for " + vm );
1632- _haMgr .scheduleStop (vm , vm . getHostId () , WorkType .Stop );
1599+ _haMgr .scheduleStop (vm , hostId , WorkType .Stop );
16331600 } else {
16341601 s_logger .debug ("VM state is in stopped so stopping it on the agent" );
16351602 command = cleanup (agentName );
16361603 }
16371604 }
16381605 return command ;
16391606 }
1607+
1608+ private void ensureVmRunningContext (long hostId , VMInstanceVO vm , Event cause ) throws OperationTimedoutException , ResourceUnavailableException , NoTransitionException {
1609+ VirtualMachineGuru <VMInstanceVO > vmGuru = getVmGuru (vm );
1610+
1611+ s_logger .debug ("VM state is starting on full sync so updating it to running" );
1612+ vm = findById (vm .getType (), vm .getId ());
1613+ try {
1614+ stateTransitTo (vm , cause , hostId );
1615+ } catch (NoTransitionException e1 ) {
1616+ s_logger .warn (e1 .getMessage ());
1617+ }
1618+
1619+ s_logger .debug ("VM's " + vm + " state is starting on full sync so updating it to Running" );
1620+ vm = vmGuru .findById (vm .getId ()); // this should ensure vm has the most up to date info
1621+
1622+ VirtualMachineProfile <VMInstanceVO > profile = new VirtualMachineProfileImpl <VMInstanceVO >(vm );
1623+ List <NicVO > nics = _nicsDao .listByVmId (profile .getId ());
1624+ for (NicVO nic : nics ) {
1625+ Network network = _networkMgr .getNetwork (nic .getNetworkId ());
1626+ NicProfile nicProfile = new NicProfile (nic , network , nic .getBroadcastUri (), nic .getIsolationUri (), null );
1627+ profile .addNic (nicProfile );
1628+ }
1629+
1630+ Commands cmds = new Commands (OnError .Stop );
1631+ s_logger .debug ("Finalizing commands that need to be send to complete Start process for the vm " + vm );
1632+
1633+ if (vmGuru .finalizeCommandsOnStart (cmds , profile )) {
1634+ if (cmds .size () != 0 ) {
1635+ _agentMgr .send (vm .getHostId (), cmds );
1636+ }
1637+
1638+ if (vmGuru .finalizeStart (profile , vm .getHostId (), cmds , null )) {
1639+ stateTransitTo (vm , cause , vm .getHostId ());
1640+ } else {
1641+ s_logger .error ("Unable to finish finialization for running vm: " + vm );
1642+ }
1643+ } else {
1644+ s_logger .error ("Unable to finalize commands on start for vm: " + vm );
1645+ }
1646+ }
16401647
16411648 public Commands fullSync (final long hostId , final Map <String , State > newStates ) {
16421649 Commands commands = new Commands (OnError .Continue );
0 commit comments