Skip to content

Commit be38384

Browse files
author
Kelven Yang
committed
bug 10414: update VM sync logic and capacity management to sync VM state change initiated from external source
1 parent d8c39cc commit be38384

3 files changed

Lines changed: 80 additions & 60 deletions

File tree

‎api/src/com/cloud/vm/VirtualMachine.java‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,19 +72,22 @@ public static StateMachine2<State, VirtualMachine.Event, VirtualMachine> getStat
7272
s_fsm.addTransition(State.Stopped, VirtualMachine.Event.OperationFailed, State.Error);
7373
s_fsm.addTransition(State.Stopped, VirtualMachine.Event.ExpungeOperation, State.Expunging);
7474
s_fsm.addTransition(State.Stopped, VirtualMachine.Event.AgentReportShutdowned, State.Stopped);
75+
s_fsm.addTransition(State.Stopped, VirtualMachine.Event.AgentReportMigrated, State.Stopped);
7576
s_fsm.addTransition(State.Starting, VirtualMachine.Event.OperationRetry, State.Starting);
7677
s_fsm.addTransition(State.Starting, VirtualMachine.Event.OperationSucceeded, State.Running);
7778
s_fsm.addTransition(State.Starting, VirtualMachine.Event.OperationFailed, State.Stopped);
7879
s_fsm.addTransition(State.Starting, VirtualMachine.Event.AgentReportRunning, State.Running);
7980
s_fsm.addTransition(State.Starting, VirtualMachine.Event.AgentReportStopped, State.Stopped);
8081
s_fsm.addTransition(State.Starting, VirtualMachine.Event.AgentReportShutdowned, State.Stopped);
82+
s_fsm.addTransition(State.Starting, VirtualMachine.Event.AgentReportMigrated, State.Starting);
8183
s_fsm.addTransition(State.Destroyed, VirtualMachine.Event.RecoveryRequested, State.Stopped);
8284
s_fsm.addTransition(State.Destroyed, VirtualMachine.Event.ExpungeOperation, State.Expunging);
8385
s_fsm.addTransition(State.Running, VirtualMachine.Event.MigrationRequested, State.Migrating);
8486
s_fsm.addTransition(State.Running, VirtualMachine.Event.AgentReportRunning, State.Running);
8587
s_fsm.addTransition(State.Running, VirtualMachine.Event.AgentReportStopped, State.Stopped);
8688
s_fsm.addTransition(State.Running, VirtualMachine.Event.StopRequested, State.Stopping);
8789
s_fsm.addTransition(State.Running, VirtualMachine.Event.AgentReportShutdowned, State.Stopped);
90+
s_fsm.addTransition(State.Running, VirtualMachine.Event.AgentReportMigrated, State.Running);
8891
s_fsm.addTransition(State.Migrating, VirtualMachine.Event.MigrationRequested, State.Migrating);
8992
s_fsm.addTransition(State.Migrating, VirtualMachine.Event.OperationSucceeded, State.Running);
9093
s_fsm.addTransition(State.Migrating, VirtualMachine.Event.OperationFailed, State.Running);
@@ -97,6 +100,7 @@ public static StateMachine2<State, VirtualMachine.Event, VirtualMachine> getStat
97100
s_fsm.addTransition(State.Stopping, VirtualMachine.Event.AgentReportStopped, State.Stopped);
98101
s_fsm.addTransition(State.Stopping, VirtualMachine.Event.StopRequested, State.Stopping);
99102
s_fsm.addTransition(State.Stopping, VirtualMachine.Event.AgentReportShutdowned, State.Stopped);
103+
s_fsm.addTransition(State.Stopping, VirtualMachine.Event.AgentReportMigrated, State.Stopping);
100104
s_fsm.addTransition(State.Expunging, VirtualMachine.Event.OperationFailed, State.Expunging);
101105
s_fsm.addTransition(State.Expunging, VirtualMachine.Event.ExpungeOperation, State.Expunging);
102106
s_fsm.addTransition(State.Error, VirtualMachine.Event.DestroyRequested, State.Expunging);
@@ -138,7 +142,8 @@ public enum Event {
138142
OperationSucceeded,
139143
OperationFailed,
140144
OperationRetry,
141-
AgentReportShutdowned
145+
AgentReportShutdowned,
146+
AgentReportMigrated
142147
};
143148

144149
public enum Type {

‎server/src/com/cloud/capacity/CapacityManagerImpl.java‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -519,10 +519,14 @@ public boolean postStateTransitionEvent(State oldState, Event event, State newSt
519519
releaseVmCapacity(vm, false, false, oldHostId);
520520
} else if (event == Event.AgentReportStopped) {
521521
releaseVmCapacity(vm, false, true, oldHostId);
522+
} else if(event == Event.AgentReportMigrated) {
523+
releaseVmCapacity(vm, false, false, oldHostId);
522524
}
523525
} else if (oldState == State.Running) {
524526
if (event == Event.AgentReportStopped) {
525527
releaseVmCapacity(vm, false, true, oldHostId);
528+
} else if(event == Event.AgentReportMigrated) {
529+
releaseVmCapacity(vm, false, false, oldHostId);
526530
}
527531
} else if (oldState == State.Migrating) {
528532
if (event == Event.AgentReportStopped) {
@@ -538,14 +542,18 @@ public boolean postStateTransitionEvent(State oldState, Event event, State newSt
538542
} else if (oldState == State.Stopping) {
539543
if (event == Event.AgentReportStopped || event == Event.OperationSucceeded) {
540544
releaseVmCapacity(vm, false, true, oldHostId);
545+
} else if(event == Event.AgentReportMigrated) {
546+
releaseVmCapacity(vm, false, false, oldHostId);
541547
}
542548
} else if (oldState == State.Stopped) {
543549
if (event == Event.DestroyRequested) {
544550
releaseVmCapacity(vm, true, false, vm.getLastHostId());
551+
} else if(event == Event.AgentReportMigrated) {
552+
releaseVmCapacity(vm, false, false, oldHostId);
545553
}
546554
}
547-
548-
if ((newState == State.Starting || newState == State.Migrating) && vm.getHostId() != null) {
555+
556+
if ((newState == State.Starting || newState == State.Migrating || event == Event.AgentReportMigrated) && vm.getHostId() != null) {
549557
boolean fromLastHost = false;
550558
if (vm.getLastHostId() == vm.getHostId()) {
551559
s_logger.debug("VM starting again on the last host it was stopped on");

‎server/src/com/cloud/vm/VirtualMachineManagerImpl.java‎

Lines changed: 64 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)