Skip to content

Commit b7cdae2

Browse files
committed
fix for migration
1 parent d17beeb commit b7cdae2

17 files changed

Lines changed: 319 additions & 74 deletions

‎api/src/com/cloud/deploy/DeploymentPlanner.java‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,5 +121,32 @@ public boolean shouldAvoid(Host host) {
121121

122122
return false;
123123
}
124+
125+
public boolean shouldAvoid(Cluster cluster) {
126+
if (_dcIds != null && _dcIds.contains(cluster.getDataCenterId())) {
127+
return true;
128+
}
129+
130+
if (_podIds != null && _podIds.contains(cluster.getPodId())) {
131+
return true;
132+
}
133+
134+
if (_clusterIds != null && _clusterIds.contains(cluster.getId())) {
135+
return true;
136+
}
137+
return false;
138+
}
139+
140+
public boolean shouldAvoid(Pod pod) {
141+
if (_dcIds != null && _dcIds.contains(pod.getDataCenterId())) {
142+
return true;
143+
}
144+
145+
if (_podIds != null && _podIds.contains(pod.getId())) {
146+
return true;
147+
}
148+
149+
return false;
150+
}
124151
}
125152
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ public enum Event {
3838
ExpungeOperation,
3939
OperationSucceeded,
4040
OperationFailed,
41+
MigrationFailedOnSource,
42+
MigrationFailedOnDest,
4143
OperationRetry,
4244
OperationCancelled
4345
};

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2159,9 +2159,9 @@ private void createCapacityEntry(final StartupCommand startup, HostVO server) {
21592159
if (capacityVOCpus != null && !capacityVOCpus.isEmpty()) {
21602160
CapacityVO CapacityVOCpu = capacityVOCpus.get(0);
21612161
long newTotalCpu = (long)(server.getCpus().longValue() * server.getSpeed().longValue()*_cpuOverProvisioningFactor);
2162-
if (CapacityVOCpu.getTotalCapacity() < newTotalCpu) {
2162+
if ((CapacityVOCpu.getTotalCapacity() <= newTotalCpu) || ((CapacityVOCpu.getUsedCapacity() + CapacityVOCpu.getReservedCapacity()) <= newTotalCpu)) {
21632163
CapacityVOCpu.setTotalCapacity(newTotalCpu);
2164-
} else if (CapacityVOCpu.getUsedCapacity() + CapacityVOCpu.getReservedCapacity() > newTotalCpu && CapacityVOCpu.getUsedCapacity() < newTotalCpu) {
2164+
} else if ((CapacityVOCpu.getUsedCapacity() + CapacityVOCpu.getReservedCapacity() > newTotalCpu) && (CapacityVOCpu.getUsedCapacity() < newTotalCpu)) {
21652165
CapacityVOCpu.setReservedCapacity(0);
21662166
CapacityVOCpu.setTotalCapacity(newTotalCpu);
21672167
} else {
@@ -2180,12 +2180,12 @@ private void createCapacityEntry(final StartupCommand startup, HostVO server) {
21802180
capacityMem.addAnd("dataCenterId", SearchCriteria.Op.EQ, server.getDataCenterId());
21812181
capacityMem.addAnd("podId", SearchCriteria.Op.EQ, server.getPodId());
21822182
capacityMem.addAnd("capacityType", SearchCriteria.Op.EQ, CapacityVO.CAPACITY_TYPE_MEMORY);
2183-
List<CapacityVO> capacityVOMems = _capacityDao.search(capacitySC, null);
2183+
List<CapacityVO> capacityVOMems = _capacityDao.search(capacityMem, null);
21842184

21852185
if (capacityVOMems != null && !capacityVOMems.isEmpty()) {
21862186
CapacityVO CapacityVOMem = capacityVOMems.get(0);
21872187
long newTotalMem = server.getTotalMemory();
2188-
if (CapacityVOMem.getTotalCapacity() < newTotalMem) {
2188+
if (CapacityVOMem.getTotalCapacity() <= newTotalMem || (CapacityVOMem.getUsedCapacity() + CapacityVOMem.getReservedCapacity() <= newTotalMem)) {
21892189
CapacityVOMem.setTotalCapacity(newTotalMem);
21902190
} else if (CapacityVOMem.getUsedCapacity() + CapacityVOMem.getReservedCapacity() > newTotalMem && CapacityVOMem.getUsedCapacity() < newTotalMem) {
21912191
CapacityVOMem.setReservedCapacity(0);

‎server/src/com/cloud/deploy/FirstFitPlanner.java‎

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package com.cloud.deploy;
22

33
import java.util.ArrayList;
4-
import java.util.Collections;
54
import java.util.List;
65

76
import javax.ejb.Local;
87

8+
import org.apache.log4j.Logger;
9+
910
import com.cloud.capacity.CapacityVO;
1011
import com.cloud.capacity.dao.CapacityDao;
1112
import com.cloud.dc.ClusterVO;
@@ -26,7 +27,6 @@
2627
import com.cloud.org.Cluster;
2728
import com.cloud.storage.GuestOSCategoryVO;
2829
import com.cloud.storage.GuestOSVO;
29-
import com.cloud.storage.VMTemplateVO;
3030
import com.cloud.storage.dao.GuestOSCategoryDao;
3131
import com.cloud.storage.dao.GuestOSDao;
3232
import com.cloud.template.VirtualMachineTemplate;
@@ -35,8 +35,10 @@
3535
import com.cloud.utils.db.Transaction;
3636
import com.cloud.vm.VirtualMachine;
3737
import com.cloud.vm.VirtualMachineProfile;
38+
3839
@Local(value=DeploymentPlanner.class)
3940
public class FirstFitPlanner extends PlannerBase implements DeploymentPlanner {
41+
private static final Logger s_logger = Logger.getLogger(FirstFitPlanner.class);
4042
@Inject private HostDao _hostDao;
4143
@Inject private CapacityDao _capacityDao;
4244
@Inject private DataCenterDao _dcDao;
@@ -56,10 +58,12 @@ public DeployDestination plan(VirtualMachineProfile vmProfile,
5658
int cpu_requested = offering.getCpu() * offering.getSpeed();
5759
long ram_requested = offering.getRamSize() * 1024L * 1024L;
5860

61+
s_logger.debug("try to allocate a host from dc:" + plan.getDataCenterId() + ", pod:" + plan.getPodId() + ",cluster:" + plan.getClusterId() +
62+
", requested cpu: " + cpu_requested + ", requested ram: " + ram_requested);
5963
if (vm.getLastHostId() != null) {
6064
HostVO host = _hostDao.findById(vm.getLastHostId());
6165

62-
if (host.getStatus() == Status.Up) {
66+
if (host != null && host.getStatus() == Status.Up) {
6367
boolean canDepployToLastHost = deployToHost(host, cpu_requested, ram_requested, true, avoid);
6468
if (canDepployToLastHost) {
6569
Pod pod = _podDao.findById(vm.getPodId());
@@ -70,29 +74,52 @@ public DeployDestination plan(VirtualMachineProfile vmProfile,
7074
}
7175

7276
/*Go through all the pods/clusters under zone*/
73-
List<HostPodVO> pods;
77+
List<HostPodVO> pods = null;
7478
if (plan.getPodId() != null) {
75-
pods = new ArrayList<HostPodVO>(1);
76-
pods.add(_podDao.findById(plan.getPodId()));
77-
} else {
78-
pods = _podDao.listByDataCenterId(plan.getDataCenterId());
79-
}
79+
HostPodVO pod = _podDao.findById(plan.getPodId());
80+
if (pod != null && dc.getId() == pod.getDataCenterId()) {
81+
pods = new ArrayList<HostPodVO>(1);
82+
pods.add(pod);
83+
} else {
84+
s_logger.debug("Can't enforce the pod selector");
85+
return null;
86+
}
87+
}
88+
89+
if (pods == null)
90+
pods = _podDao.listByDataCenterId(plan.getDataCenterId());
91+
8092
//Collections.shuffle(pods);
8193

8294
for (HostPodVO hostPod : pods) {
95+
if (avoid.shouldAvoid(hostPod)) {
96+
continue;
97+
}
8398

8499
//Collections.shuffle(clusters);
85-
List<ClusterVO> clusters;
100+
List<ClusterVO> clusters = null;
86101
if (plan.getClusterId() != null) {
87-
clusters = new ArrayList<ClusterVO>(1);
88-
clusters.add(_clusterDao.findById(plan.getClusterId()));
89-
} else {
102+
ClusterVO cluster = _clusterDao.findById(plan.getClusterId());
103+
if (cluster != null && hostPod.getId() == cluster.getPodId()) {
104+
clusters = new ArrayList<ClusterVO>(1);
105+
clusters.add(cluster);
106+
} else {
107+
s_logger.debug("Can't enforce the cluster selector");
108+
return null;
109+
}
110+
}
111+
112+
if (clusters == null) {
90113
clusters = _clusterDao.listByPodId(hostPod.getId());
91114
}
92115

93116
for (ClusterVO clusterVO : clusters) {
117+
if (avoid.shouldAvoid(clusterVO)) {
118+
continue;
119+
}
94120

95121
if (clusterVO.getHypervisorType() != vmProfile.getHypervisorType()) {
122+
avoid.addCluster(clusterVO.getId());
96123
continue;
97124
}
98125

@@ -111,8 +138,11 @@ public DeployDestination plan(VirtualMachineProfile vmProfile,
111138
Host host = _hostDao.findById(hostVO.getId());
112139
return new DeployDestination(dc, pod, cluster, host);
113140
}
141+
avoid.addHost(hostVO.getId());
114142
}
143+
avoid.addCluster(clusterVO.getId());
115144
}
145+
avoid.addPod(hostPod.getId());
116146
}
117147

118148
return null;

‎server/src/com/cloud/ha/HighAvailabilityManagerImpl.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -777,7 +777,7 @@ public Long migrate(final HaWorkVO work) {
777777
HostPodVO podVO = _podDao.findById(vm.getPodId());
778778
_alertMgr.sendAlert(alertType, fromHost.getDataCenterId(), fromHost.getPodId(), "Unable to migrate vm " + vm.getHostName() + " from host " + fromHost.getName() + " in zone " + dcVO.getName() + " and pod " + podVO.getName(), "Migrate Command failed. Please check logs.");
779779

780-
_itMgr.stateTransitTo(vm, Event.OperationFailed, vm.getHostId());
780+
_itMgr.stateTransitTo(vm, Event.MigrationFailedOnSource, toHost.getId());
781781
_agentMgr.maintenanceFailed(vm.getHostId());
782782

783783
Command cleanup = mgr.cleanup(vm, null);
@@ -807,7 +807,7 @@ public Long migrate(final HaWorkVO work) {
807807
} catch (final OperationTimedoutException e) {
808808
s_logger.warn("Operation timed outfor " + vm.toString());
809809
}
810-
_itMgr.stateTransitTo(vm, Event.OperationFailed, toHost.getId());
810+
_itMgr.stateTransitTo(vm, Event.MigrationFailedOnDest, toHost.getId());
811811
return (System.currentTimeMillis() >> 10) + _migrateRetryInterval;
812812
}
813813

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

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@
9494
import com.cloud.utils.fsm.StateMachine2;
9595
import com.cloud.vm.ItWorkVO.Type;
9696
import com.cloud.vm.VirtualMachine.Event;
97+
import com.cloud.vm.dao.ConsoleProxyDao;
98+
import com.cloud.vm.dao.DomainRouterDao;
99+
import com.cloud.vm.dao.SecondaryStorageVmDao;
100+
import com.cloud.vm.dao.UserVmDao;
97101
import com.cloud.vm.dao.VMInstanceDao;
98102

99103
@Local(value=VmManager.class)
@@ -113,6 +117,10 @@ public class MauriceMoss implements VmManager, ClusterManagerListener {
113117
@Inject private ClusterManager _clusterMgr;
114118
@Inject private ItWorkDao _workDao;
115119
@Inject private CapacityDao _capacityDao;
120+
@Inject private UserVmDao _userVmDao;
121+
@Inject private DomainRouterDao _routerDao;
122+
@Inject private ConsoleProxyDao _consoleDao;
123+
@Inject private SecondaryStorageVmDao _secondaryDao;
116124

117125
@Inject(adapter=DeploymentPlanner.class)
118126
private Adapters<DeploymentPlanner> _planners;
@@ -340,13 +348,15 @@ public <T extends VMInstanceVO> T start(T vm, Map<String, Object> params, User c
340348

341349
ExcludeList avoids = new ExcludeList();
342350
int retry = _retry;
343-
while (retry-- != 0) { // It's != so that it can match -1.
351+
DeployDestination dest = null;
352+
while (retry-- != 0) { // It's != so that it can match -1.
353+
/*this will release resource allocated on dest host*/
344354
if (retry < (_retry -1)) {
345-
stateTransitTo(vm, Event.OperationRetry, null);
355+
stateTransitTo(vm, Event.OperationRetry, dest.getHost().getId());
346356
}
347357

348358
VirtualMachineProfileImpl<T> vmProfile = new VirtualMachineProfileImpl<T>(vm, template, offering, null, params);
349-
DeployDestination dest = null;
359+
350360
for (DeploymentPlanner planner : _planners) {
351361
dest = planner.plan(vmProfile, plan, avoids);
352362
if (dest != null) {
@@ -366,7 +376,7 @@ public <T extends VMInstanceVO> T start(T vm, Map<String, Object> params, User c
366376
try {
367377
_storageMgr.prepare(vmProfile, dest);
368378
} catch (ConcurrentOperationException e) {
369-
stateTransitTo(vm, Event.OperationFailed, null);
379+
stateTransitTo(vm, Event.OperationFailed, dest.getHost().getId());
370380
throw e;
371381
} catch (StorageUnavailableException e) {
372382
s_logger.warn("Unable to contact storage.", e);
@@ -401,7 +411,7 @@ public <T extends VMInstanceVO> T start(T vm, Map<String, Object> params, User c
401411
}
402412
}
403413

404-
stateTransitTo(vm, Event.OperationFailed, null);
414+
stateTransitTo(vm, Event.OperationFailed, dest.getHost().getId());
405415

406416
if (s_logger.isDebugEnabled()) {
407417
s_logger.debug("Creation complete for VM " + vm);
@@ -463,7 +473,7 @@ public <T extends VMInstanceVO> boolean stop(T vm, User user, Account account) t
463473
}
464474

465475
boolean cleanup = false;
466-
476+
467477
VirtualMachineProfile<T> profile = new VirtualMachineProfileImpl<T>(vm);
468478
try {
469479
_networkMgr.release(profile);
@@ -510,7 +520,7 @@ public void onManagementNodeLeft(List<ManagementServerHostVO> nodeList, long sel
510520
}
511521

512522
private void setStateMachine() {
513-
_stateMachine = new StateMachine2<State, VirtualMachine.Event, VMInstanceVO >(_vmDao);
523+
_stateMachine = new StateMachine2<State, VirtualMachine.Event, VMInstanceVO>();
514524

515525
_stateMachine.addTransition(null, VirtualMachine.Event.CreateRequested, State.Creating);
516526
_stateMachine.addTransition(State.Creating, VirtualMachine.Event.OperationSucceeded, State.Stopped);
@@ -534,6 +544,8 @@ private void setStateMachine() {
534544
_stateMachine.addTransition(State.Migrating, VirtualMachine.Event.MigrationRequested, State.Migrating);
535545
_stateMachine.addTransition(State.Migrating, VirtualMachine.Event.OperationSucceeded, State.Running);
536546
_stateMachine.addTransition(State.Migrating, VirtualMachine.Event.OperationFailed, State.Running);
547+
_stateMachine.addTransition(State.Migrating, VirtualMachine.Event.MigrationFailedOnSource, State.Running);
548+
_stateMachine.addTransition(State.Migrating, VirtualMachine.Event.MigrationFailedOnDest, State.Running);
537549
_stateMachine.addTransition(State.Migrating, VirtualMachine.Event.AgentReportRunning, State.Running);
538550
_stateMachine.addTransition(State.Migrating, VirtualMachine.Event.AgentReportStopped, State.Stopped);
539551
_stateMachine.addTransition(State.Stopping, VirtualMachine.Event.OperationSucceeded, State.Stopped);
@@ -544,11 +556,20 @@ private void setStateMachine() {
544556
_stateMachine.addTransition(State.Expunging, VirtualMachine.Event.OperationFailed, State.Expunging);
545557
_stateMachine.addTransition(State.Expunging, VirtualMachine.Event.ExpungeOperation, State.Expunging);
546558

547-
_stateMachine.registerListener(new VMStateListener(_capacityDao, _offeringDao, _vmDao));
559+
_stateMachine.registerListener(new VMStateListener(_capacityDao, _offeringDao));
548560
}
549561

550562
@Override
551563
public boolean stateTransitTo(VMInstanceVO vm, VirtualMachine.Event e, Long id) {
552-
return _stateMachine.transitTO(vm, e, id);
564+
if (vm instanceof UserVmVO) {
565+
return _stateMachine.transitTO(vm, e, id, _userVmDao);
566+
} else if (vm instanceof ConsoleProxyVO) {
567+
return _stateMachine.transitTO(vm, e, id, _consoleDao);
568+
} else if (vm instanceof SecondaryStorageVmVO) {
569+
return _stateMachine.transitTO(vm, e, id, _secondaryDao);
570+
} else if (vm instanceof DomainRouterVO) {
571+
return _stateMachine.transitTO(vm, e, id, _routerDao);
572+
}
573+
return false;
553574
}
554575
}

0 commit comments

Comments
 (0)