Skip to content

Commit ce44bc7

Browse files
committed
#4534 extract some codes to methods
1 parent 2bfeed0 commit ce44bc7

File tree

3 files changed

+48
-34
lines changed

3 files changed

+48
-34
lines changed

engine/orchestration/src/main/java/com/cloud/vm/VirtualMachineManagerImpl.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3230,12 +3230,12 @@ public boolean checkIfVmHasClusterWideVolumes(Long vmId) {
32303230
public DataCenterDeployment getMigrationDeployment(final Long vmId, final Host host, final Long poolId, final ExcludeList excludes) {
32313231
if (MIGRATE_VM_ACROSS_CLUSTERS.valueIn(host.getDataCenterId()) && !checkIfVmHasClusterWideVolumes(vmId)) {
32323232
s_logger.info("Searching for hosts in the zone for vm migration");
3233-
List<Long> clustersToExcluded = _clusterDao.listAllClusters(host.getDataCenterId());
3233+
List<Long> clustersToExclude = _clusterDao.listAllClusters(host.getDataCenterId());
32343234
List<ClusterVO> clusterList = _clusterDao.listByDcHyType(host.getDataCenterId(), host.getHypervisorType().toString());
32353235
for (ClusterVO cluster : clusterList) {
3236-
clustersToExcluded.remove(cluster.getId());
3236+
clustersToExclude.remove(cluster.getId());
32373237
}
3238-
for (Long clusterId : clustersToExcluded) {
3238+
for (Long clusterId : clustersToExclude) {
32393239
excludes.addCluster(clusterId);
32403240
}
32413241
return new DataCenterDeployment(host.getDataCenterId(), null, null, null, poolId, null);

server/src/main/java/com/cloud/resource/ResourceManagerImpl.java

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1266,22 +1266,7 @@ private boolean doMaintain(final long hostId) {
12661266
List<HostVO> hosts = listAllUpAndEnabledHosts(Host.Type.Routing, host.getClusterId(), host.getPodId(), host.getDataCenterId());
12671267
if (hosts == null || hosts.isEmpty()) {
12681268
s_logger.warn("Unable to find a host for vm migration in cluster: " + host.getClusterId());
1269-
if (MIGRATE_VM_ACROSS_CLUSTERS.valueIn(host.getDataCenterId())) {
1270-
s_logger.info("Looking for hosts across different clusters in zone: " + host.getDataCenterId());
1271-
hosts = listAllUpAndEnabledHosts(Host.Type.Routing, null, null, host.getDataCenterId());
1272-
if (hosts == null || hosts.isEmpty()) {
1273-
s_logger.warn("Unable to find a host for vm migration in zone: " + host.getDataCenterId());
1274-
return false;
1275-
}
1276-
// Dont migrate vm if it has volumes on cluster-wide pool
1277-
for (final VMInstanceVO vm : vms) {
1278-
if (_vmMgr.checkIfVmHasClusterWideVolumes(vm.getId())) {
1279-
s_logger.warn("Unable to migrate vm " + vm.getInstanceName() + " as it has volumes on cluster-wide pool");
1280-
return false;
1281-
}
1282-
}
1283-
} else {
1284-
s_logger.warn("Not migrating VM across cluster since " + MIGRATE_VM_ACROSS_CLUSTERS.key() + " is false");
1269+
if (! isClusterWideMigrationSupported(host, vms, hosts)) {
12851270
return false;
12861271
}
12871272
}
@@ -1305,6 +1290,28 @@ private boolean doMaintain(final long hostId) {
13051290
return true;
13061291
}
13071292

1293+
private boolean isClusterWideMigrationSupported(Host host, List<VMInstanceVO> vms, List<HostVO> hosts) {
1294+
if (MIGRATE_VM_ACROSS_CLUSTERS.valueIn(host.getDataCenterId())) {
1295+
s_logger.info("Looking for hosts across different clusters in zone: " + host.getDataCenterId());
1296+
hosts = listAllUpAndEnabledHosts(Host.Type.Routing, null, null, host.getDataCenterId());
1297+
if (hosts == null || hosts.isEmpty()) {
1298+
s_logger.warn("Unable to find a host for vm migration in zone: " + host.getDataCenterId());
1299+
return false;
1300+
}
1301+
// Dont migrate vm if it has volumes on cluster-wide pool
1302+
for (final VMInstanceVO vm : vms) {
1303+
if (_vmMgr.checkIfVmHasClusterWideVolumes(vm.getId())) {
1304+
s_logger.warn("Unable to migrate vm " + vm.getInstanceName() + " as it has volumes on cluster-wide pool");
1305+
return false;
1306+
}
1307+
}
1308+
} else {
1309+
s_logger.warn("Not migrating VM across cluster since " + MIGRATE_VM_ACROSS_CLUSTERS.key() + " is false");
1310+
return false;
1311+
}
1312+
return true;
1313+
}
1314+
13081315
@Override
13091316
public boolean maintain(final long hostId) throws AgentUnavailableException {
13101317
final Boolean result = propagateResourceEvent(hostId, ResourceState.Event.AdminAskMaintenance);

server/src/main/java/com/cloud/vm/UserVmManagerImpl.java

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5792,21 +5792,7 @@ public VirtualMachine migrateVirtualMachine(Long vmId, Host destinationHost) thr
57925792

57935793
DeployDestination dest = null;
57945794
if (destinationHost == null) {
5795-
vm.setLastHostId(null); // Last host does not have higher priority in vm migration
5796-
final ServiceOfferingVO offering = _offeringDao.findById(vm.getId(), vm.getServiceOfferingId());
5797-
final VirtualMachineProfile profile = new VirtualMachineProfileImpl(vm, null, offering, null, null);
5798-
final Host host = _hostDao.findById(srcHostId);
5799-
ExcludeList excludes = new ExcludeList();
5800-
excludes.addHost(srcHostId);
5801-
final DataCenterDeployment plan = _itMgr.getMigrationDeployment(vm.getId(), host, null, excludes);
5802-
try {
5803-
dest = _planningMgr.planDeployment(profile, plan, excludes, null);
5804-
} catch (final AffinityConflictException e2) {
5805-
s_logger.warn("Unable to create deployment, affinity rules associted to the VM conflict", e2);
5806-
throw new CloudRuntimeException("Unable to create deployment, affinity rules associted to the VM conflict");
5807-
} catch (final InsufficientServerCapacityException e3) {
5808-
throw new CloudRuntimeException("Unable to find a server to migrate the vm to");
5809-
}
5795+
dest = findVmMigrationDestination(vm, srcHostId);
58105796
} else {
58115797
dest = checkVmMigrationDestination(vm, srcHostId, destinationHost);
58125798
}
@@ -5830,6 +5816,27 @@ public VirtualMachine migrateVirtualMachine(Long vmId, Host destinationHost) thr
58305816
}
58315817
}
58325818

5819+
private DeployDestination findVmMigrationDestination(VMInstanceVO vm, Long srcHostId) {
5820+
DeployDestination dest = null;
5821+
vm.setLastHostId(null); // Last host does not have higher priority in vm migration
5822+
final ServiceOfferingVO offering = _offeringDao.findById(vm.getId(), vm.getServiceOfferingId());
5823+
final VirtualMachineProfile profile = new VirtualMachineProfileImpl(vm, null, offering, null, null);
5824+
final Host host = _hostDao.findById(srcHostId);
5825+
ExcludeList excludes = new ExcludeList();
5826+
excludes.addHost(srcHostId);
5827+
final DataCenterDeployment plan = _itMgr.getMigrationDeployment(vm.getId(), host, null, excludes);
5828+
try {
5829+
dest = _planningMgr.planDeployment(profile, plan, excludes, null);
5830+
} catch (final AffinityConflictException e2) {
5831+
s_logger.warn("Unable to create deployment, affinity rules associted to the VM conflict", e2);
5832+
throw new CloudRuntimeException("Unable to create deployment, affinity rules associted to the VM conflict");
5833+
} catch (final InsufficientServerCapacityException e3) {
5834+
throw new CloudRuntimeException("Unable to find a server to migrate the vm to");
5835+
}
5836+
return dest;
5837+
}
5838+
5839+
58335840
private DeployDestination checkVmMigrationDestination(VMInstanceVO vm, Long srcHostId, Host destinationHost) throws VirtualMachineMigrationException {
58345841
if (destinationHost == null) {
58355842
return null;

0 commit comments

Comments
 (0)