Skip to content

Commit d17beeb

Browse files
committed
move all the vm's state machine to itmgr->statetransitTO
1 parent 2e3ef14 commit d17beeb

24 files changed

Lines changed: 340 additions & 117 deletions

‎server/src/com/cloud/agent/AgentManager.java‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import com.cloud.exception.InvalidParameterValueException;
3232
import com.cloud.exception.OperationTimedoutException;
3333
import com.cloud.host.Host;
34+
import com.cloud.host.Host.Type;
3435
import com.cloud.host.HostStats;
3536
import com.cloud.host.HostVO;
3637
import com.cloud.host.Status;
@@ -155,7 +156,7 @@ public enum OnError {
155156
* to deploy in, service offering, template, and list of host to avoid.
156157
*/
157158

158-
Host findHost(Host.Type type, DataCenterVO dc, HostPodVO pod, StoragePoolVO sp, ServiceOffering offering, VMTemplateVO template, VMInstanceVO vm, Host currentHost, Set<Host> avoid);
159+
Host findHost(Host.Type type, DataCenterVO dc, HostPodVO pod, StoragePoolVO sp, ServiceOfferingVO offering, VMTemplateVO template, VMInstanceVO vm, Host currentHost, Set<Host> avoid);
159160
List<PodCluster> listByDataCenter(long dcId);
160161
List<PodCluster> listByPod(long podId);
161162

@@ -212,4 +213,5 @@ public enum OnError {
212213
public List<HostVO> discoverHosts(Long dcId, Long podId, Long clusterId, String clusterName, String url, String username, String password) throws IllegalArgumentException, DiscoveryException, InvalidParameterValueException;
213214

214215
Answer easySend(Long hostId, Command cmd, int timeout);
216+
215217
}

‎server/src/com/cloud/agent/manager/AgentManagerImpl.java‎

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,15 @@
9595
import com.cloud.dc.dao.DataCenterIpAddressDao;
9696
import com.cloud.dc.dao.HostPodDao;
9797
import com.cloud.dc.dao.VlanDao;
98+
import com.cloud.deploy.DataCenterDeployment;
99+
import com.cloud.deploy.DeployDestination;
100+
import com.cloud.deploy.DeploymentPlanner;
101+
import com.cloud.deploy.DeploymentPlanner.ExcludeList;
98102
import com.cloud.event.dao.EventDao;
99103
import com.cloud.exception.AgentUnavailableException;
100104
import com.cloud.exception.ConnectionException;
101105
import com.cloud.exception.DiscoveryException;
106+
import com.cloud.exception.InsufficientServerCapacityException;
102107
import com.cloud.exception.InvalidParameterValueException;
103108
import com.cloud.exception.OperationTimedoutException;
104109
import com.cloud.exception.UnsupportedVersionException;
@@ -218,6 +223,9 @@ public class AgentManagerImpl implements AgentManager, HandlerFactory, ResourceS
218223
@Inject protected DetailsDao _hostDetailsDao = null;
219224
@Inject protected ClusterDao _clusterDao;
220225

226+
@Inject(adapter=DeploymentPlanner.class)
227+
private Adapters<DeploymentPlanner> _planners;
228+
221229
protected Adapters<Discoverer> _discoverers = null;
222230
protected int _port;
223231

@@ -434,24 +442,33 @@ public Set<Long> getConnectedHosts() {
434442

435443
@Override
436444
public Host findHost(final Host.Type type, final DataCenterVO dc, final HostPodVO pod, final StoragePoolVO sp,
437-
final ServiceOffering offering, final VMTemplateVO template, VMInstanceVO vm,
445+
final ServiceOfferingVO offering, final VMTemplateVO template, VMInstanceVO vm,
438446
Host currentHost, final Set<Host> avoid) {
439-
VirtualMachineProfile<VMInstanceVO> vmc = new VirtualMachineProfileImpl<VMInstanceVO>(vm.getType());
440-
Enumeration<HostAllocator> en = _hostAllocators.enumeration();
441-
while (en.hasMoreElements()) {
442-
final HostAllocator allocator = en.nextElement();
443-
final Host host = allocator.allocateTo(vmc, offering, type, dc, pod, sp.getClusterId(), template, avoid);
444-
if (host == null) {
445-
continue;
446-
} else {
447-
return host;
448-
}
447+
VirtualMachineProfileImpl<VMInstanceVO> vmProfile = new VirtualMachineProfileImpl<VMInstanceVO>(vm, template, offering, null, null);
448+
DeployDestination dest = null;
449+
DataCenterDeployment plan = new DataCenterDeployment(dc.getId(), pod.getId(), sp.getClusterId(), null);
450+
ExcludeList avoids = new ExcludeList();
451+
for (Host h : avoid) {
452+
avoids.addHost(h.getId());
453+
}
454+
455+
for (DeploymentPlanner planner : _planners) {
456+
try {
457+
dest = planner.plan(vmProfile, plan, avoids);
458+
if (dest != null) {
459+
return dest.getHost();
460+
}
461+
} catch (InsufficientServerCapacityException e) {
462+
463+
}
464+
449465
}
450466

451467
s_logger.warn("findHost() could not find a non-null host.");
452468
return null;
453469
}
454470

471+
455472
@Override
456473
public List<PodCluster> listByDataCenter(long dcId) {
457474
List<HostPodVO> pods = _podDao.listByDataCenterId(dcId);

‎server/src/com/cloud/async/AsyncJobExecutorContext.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import com.cloud.user.dao.UserDao;
3333
import com.cloud.utils.component.Manager;
3434
import com.cloud.vm.UserVmManager;
35+
import com.cloud.vm.VmManager;
3536
import com.cloud.vm.dao.DomainRouterDao;
3637
import com.cloud.vm.dao.UserVmDao;
3738

@@ -51,4 +52,5 @@ public interface AsyncJobExecutorContext extends Manager {
5152
public IPAddressDao getIpAddressDao();
5253
public AsyncJobDao getJobDao();
5354
public UserDao getUserDao();
55+
public VmManager getItMgr();
5456
}

‎server/src/com/cloud/async/AsyncJobExecutorContextImpl.java‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import com.cloud.user.dao.UserDao;
3838
import com.cloud.utils.component.ComponentLocator;
3939
import com.cloud.vm.UserVmManager;
40+
import com.cloud.vm.VmManager;
4041
import com.cloud.vm.dao.DomainRouterDao;
4142
import com.cloud.vm.dao.UserVmDao;
4243

@@ -57,7 +58,8 @@ public class AsyncJobExecutorContextImpl implements AsyncJobExecutorContext {
5758
private DomainRouterDao _routerDao;
5859
private IPAddressDao _ipAddressDao;
5960
private AsyncJobDao _jobDao;
60-
private UserDao _userDao;
61+
private UserDao _userDao;
62+
private VmManager _itMgr;
6163

6264
private ManagementServer _managementServer;
6365

@@ -138,6 +140,11 @@ public AsyncJobDao getJobDao() {
138140
public UserDao getUserDao() {
139141
return _userDao;
140142
}
143+
144+
@Override
145+
public VmManager getItMgr() {
146+
return _itMgr;
147+
}
141148

142149
@Override
143150
public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
@@ -218,7 +225,11 @@ public boolean configure(String name, Map<String, Object> params) throws Configu
218225
if(_userDao == null) {
219226
throw new ConfigurationException("unable to get " + UserDao.class.getName());
220227
}
221-
228+
229+
_itMgr = locator.getManager(VmManager.class);
230+
if (_itMgr == null) {
231+
throw new ConfigurationException("unable to get " + VmManager.class.getName());
232+
}
222233
return true;
223234
}
224235

‎server/src/com/cloud/async/executor/DestroyVMExecutor.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public void processAnswer(VMOperationListener listener, long agentId, long seq,
100100
txn.start();
101101

102102
asyncMgr.getExecutorContext().getAccountMgr().decrementResourceCount(vm.getAccountId(), ResourceType.user_vm);
103-
if (!asyncMgr.getExecutorContext().getVmDao().updateIf(vm, VirtualMachine.Event.DestroyRequested, vm.getHostId())) {
103+
if (!asyncMgr.getExecutorContext().getItMgr().stateTransitTo(vm, VirtualMachine.Event.DestroyRequested, vm.getHostId())) {
104104
s_logger.debug("Unable to destroy the vm because it is not in the correct state: " + vm.toString());
105105

106106
txn.rollback();
@@ -130,7 +130,7 @@ public void processAnswer(VMOperationListener listener, long agentId, long seq,
130130

131131
asyncMgr.completeAsyncJob(getJob().getId(), AsyncJobResult.STATUS_SUCCEEDED, 0, "success");
132132
} else {
133-
asyncMgr.getExecutorContext().getVmDao().updateIf(vm, Event.OperationFailed, vm.getHostId());
133+
asyncMgr.getExecutorContext().getItMgr().stateTransitTo(vm, Event.OperationFailed, vm.getHostId());
134134
asyncMgr.completeAsyncJob(getJob().getId(),
135135
AsyncJobResult.STATUS_FAILED, BaseCmd.INTERNAL_ERROR, "Agent failed to stop VM: " + vm.getHostName());
136136
// managementServer.saveEvent(param.getUserId(), vm.getAccountId(), EventVO.LEVEL_ERROR, EventTypes.EVENT_VM_STOP,

‎server/src/com/cloud/async/executor/StopVMExecutor.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public void processAnswer(VMOperationListener listener, long agentId, long seq,
9292
AsyncJobResult.STATUS_SUCCEEDED, 0, VMExecutorHelper.composeResultObject(asyncMgr.getExecutorContext().getManagementServer(), vm, null));
9393
jobStatusUpdated = true;
9494
} else {
95-
asyncMgr.getExecutorContext().getVmDao().updateIf(vm, Event.OperationFailed, vm.getHostId());
95+
asyncMgr.getExecutorContext().getItMgr().stateTransitTo(vm, Event.OperationFailed, vm.getHostId());
9696
asyncMgr.completeAsyncJob(getJob().getId(),
9797
AsyncJobResult.STATUS_FAILED, BaseCmd.INTERNAL_ERROR, "Agent failed to stop VM");
9898
jobStatusUpdated = true;

‎server/src/com/cloud/consoleproxy/ConsoleProxyManagerImpl.java‎

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,7 @@ public ConsoleProxyVO start(long proxyId, long startEventId) throws Insufficient
632632
}
633633
}
634634
// to ensure atomic state transition to Starting state
635-
if (!_consoleProxyDao.updateIf(proxy, com.cloud.vm.VirtualMachine.Event.StartRequested, routingHost.getId())) {
635+
if (!_itMgr.stateTransitTo(proxy, com.cloud.vm.VirtualMachine.Event.StartRequested, routingHost.getId())) {
636636
if (s_logger.isDebugEnabled()) {
637637
ConsoleProxyVO temp = _consoleProxyDao.findById(proxyId);
638638
s_logger.debug("Unable to start console proxy " + proxy.getHostName() + " because it is not in a startable state : "
@@ -664,7 +664,7 @@ public ConsoleProxyVO start(long proxyId, long startEventId) throws Insufficient
664664
String guestIpAddress = _dcDao.allocateLinkLocalIpAddress(proxy.getDataCenterId(), routingHost.getPodId(), proxy.getId(), null);
665665
proxy.setGuestIpAddress(guestIpAddress);
666666

667-
_consoleProxyDao.updateIf(proxy, VirtualMachine.Event.OperationRetry, routingHost.getId());
667+
_itMgr.stateTransitTo(proxy, VirtualMachine.Event.OperationRetry, routingHost.getId());
668668
proxy = _consoleProxyDao.findById(proxy.getId());
669669

670670
List<VolumeVO> vols = _storageMgr.prepare(proxy, routingHost);
@@ -768,7 +768,7 @@ public ConsoleProxyVO start(long proxyId, long startEventId) throws Insufficient
768768
throw new ExecutionException("Couldn't find a routingHost to run console proxy");
769769
}
770770

771-
_consoleProxyDao.updateIf(proxy, VirtualMachine.Event.OperationSucceeded, routingHost.getId());
771+
_itMgr.stateTransitTo(proxy, VirtualMachine.Event.OperationSucceeded, routingHost.getId());
772772
if (s_logger.isDebugEnabled()) {
773773
s_logger.debug("Console proxy is now started, vm id : " + proxy.getId());
774774
}
@@ -817,7 +817,7 @@ public ConsoleProxyVO start(long proxyId, long startEventId) throws Insufficient
817817
freePrivateIpAddress(privateIpAddress, proxy.getDataCenterId(), proxy.getId());
818818
}
819819

820-
_consoleProxyDao.updateIf(proxy, VirtualMachine.Event.OperationFailed, null);
820+
_itMgr.stateTransitTo(proxy, VirtualMachine.Event.OperationFailed, null);
821821
txn.commit();
822822
} catch (Exception e) {
823823
s_logger.error("Caught exception during error recovery");
@@ -1120,7 +1120,7 @@ protected ConsoleProxyVO allocProxyStorage(long dataCenterId, long proxyVmId) {
11201120
_consoleProxyDao.update(proxy.getId(), vo);
11211121

11221122
// kick the state machine
1123-
_consoleProxyDao.updateIf(proxy, VirtualMachine.Event.OperationSucceeded, null);
1123+
_itMgr.stateTransitTo(proxy, VirtualMachine.Event.OperationSucceeded, null);
11241124

11251125
txn.commit();
11261126
return proxy;
@@ -1788,7 +1788,7 @@ public Command cleanup(ConsoleProxyVO vm, String vmName) {
17881788

17891789
@Override
17901790
public void completeStartCommand(ConsoleProxyVO vm) {
1791-
_consoleProxyDao.updateIf(vm, VirtualMachine.Event.AgentReportRunning, vm.getHostId());
1791+
_itMgr.stateTransitTo(vm, VirtualMachine.Event.AgentReportRunning, vm.getHostId());
17921792
}
17931793

17941794
@Override
@@ -1812,7 +1812,7 @@ protected void completeStopCommand(ConsoleProxyVO proxy, VirtualMachine.Event ev
18121812
_dcDao.releaseLinkLocalIpAddress(guestIpAddress, proxy.getDataCenterId(), proxy.getId());
18131813
}
18141814

1815-
if (!_consoleProxyDao.updateIf(proxy, ev, null)) {
1815+
if (!_itMgr.stateTransitTo(proxy, ev, null)) {
18161816
s_logger.debug("Unable to update the console proxy");
18171817
return;
18181818
}
@@ -1969,7 +1969,7 @@ public boolean destroyProxy(long vmId, long startEventId) {
19691969
s_logger.debug("Destroying console proxy vm " + vmId);
19701970
}
19711971

1972-
if (!_consoleProxyDao.updateIf(vm, VirtualMachine.Event.DestroyRequested, null)) {
1972+
if (!_itMgr.stateTransitTo(vm, VirtualMachine.Event.DestroyRequested, null)) {
19731973
s_logger.debug("Unable to destroy the vm because it is not in the correct state: " + vmId);
19741974
return false;
19751975
}
@@ -2050,7 +2050,7 @@ public boolean destroyProxyDBOnly(long vmId) {
20502050

20512051
@Override
20522052
public boolean stop(ConsoleProxyVO proxy, long startEventId) throws AgentUnavailableException {
2053-
if (!_consoleProxyDao.updateIf(proxy, VirtualMachine.Event.StopRequested, proxy.getHostId())) {
2053+
if (!_itMgr.stateTransitTo(proxy, VirtualMachine.Event.StopRequested, proxy.getHostId())) {
20542054
s_logger.debug("Unable to stop console proxy: " + proxy.toString());
20552055
return false;
20562056
}
@@ -2129,7 +2129,7 @@ public boolean stop(ConsoleProxyVO proxy, long startEventId) throws AgentUnavail
21292129
public boolean migrate(ConsoleProxyVO proxy, HostVO host) {
21302130
HostVO fromHost = _hostDao.findById(proxy.getId());
21312131

2132-
if (!_consoleProxyDao.updateIf(proxy, VirtualMachine.Event.MigrationRequested, proxy.getHostId())) {
2132+
if (! _itMgr.stateTransitTo(proxy, VirtualMachine.Event.MigrationRequested, proxy.getHostId())) {
21332133
s_logger.debug("State for " + proxy.toString() + " has changed so migration can not take place.");
21342134
return false;
21352135
}
@@ -2152,18 +2152,18 @@ public boolean completeMigration(ConsoleProxyVO proxy, HostVO host) throws Agent
21522152
CheckVirtualMachineAnswer answer = (CheckVirtualMachineAnswer) _agentMgr.send(host.getId(), cvm);
21532153
if (!answer.getResult()) {
21542154
s_logger.debug("Unable to complete migration for " + proxy.getId());
2155-
_consoleProxyDao.updateIf(proxy, VirtualMachine.Event.AgentReportStopped, null);
2155+
_itMgr.stateTransitTo(proxy, VirtualMachine.Event.AgentReportStopped, null);
21562156
return false;
21572157
}
21582158

21592159
State state = answer.getState();
21602160
if (state == State.Stopped) {
21612161
s_logger.warn("Unable to complete migration as we can not detect it on " + host.getId());
2162-
_consoleProxyDao.updateIf(proxy, VirtualMachine.Event.AgentReportStopped, null);
2162+
_itMgr.stateTransitTo(proxy, VirtualMachine.Event.AgentReportStopped, null);
21632163
return false;
21642164
}
21652165

2166-
_consoleProxyDao.updateIf(proxy, VirtualMachine.Event.OperationSucceeded, host.getId());
2166+
_itMgr.stateTransitTo(proxy, VirtualMachine.Event.OperationSucceeded, host.getId());
21672167
return true;
21682168
}
21692169

0 commit comments

Comments
 (0)