-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Fix for VMware VM migration with volume in local storage #6483
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
sureshanaparti
merged 6 commits into
4.17
from
VMwareVMMigrationWithVolumeInLocalStorage
Jul 1, 2022
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
50ca91a
Fix VMware VM migration with volume in case of local storage
harikrishna-patnala 8c53536
Break the loop once target host is found
harikrishna-patnala 9daca66
Code optimisations in getting the target host guid for local storage
harikrishna-patnala e87fd13
Fixed code smells and added unit test
harikrishna-patnala 2f1702f
few variable changes
harikrishna-patnala 5b7b05e
Added a comment
harikrishna-patnala File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,6 +27,8 @@ | |
|
|
||
| import javax.inject.Inject; | ||
|
|
||
| import com.cloud.storage.StoragePoolHostVO; | ||
| import com.cloud.storage.dao.StoragePoolHostDao; | ||
| import org.apache.cloudstack.acl.ControlledEntity; | ||
| import org.apache.cloudstack.backup.Backup; | ||
| import org.apache.cloudstack.engine.subsystem.api.storage.ObjectInDataStoreStateMachine; | ||
|
|
@@ -184,6 +186,7 @@ public class VMwareGuru extends HypervisorGuruBase implements HypervisorGuru, Co | |
| @Inject UserVmDao userVmDao; | ||
| @Inject DiskOfferingDao diskOfferingDao; | ||
| @Inject PhysicalNetworkDao physicalNetworkDao; | ||
| @Inject StoragePoolHostDao storagePoolHostDao; | ||
|
|
||
| protected VMwareGuru() { | ||
| super(); | ||
|
|
@@ -1070,6 +1073,13 @@ private boolean isInterClusterMigration(Long srcClusterId, Long destClusterId) { | |
| return srcClusterId != null && destClusterId != null && ! srcClusterId.equals(destClusterId); | ||
| } | ||
|
|
||
| private String getHostGuidForLocalStorage(StoragePool pool) { | ||
| List<StoragePoolHostVO> storagePoolHostVOs = storagePoolHostDao.listByPoolId(pool.getId()); | ||
| StoragePoolHostVO storagePoolHostVO = storagePoolHostVOs.get(0); | ||
| HostVO hostVO = _hostDao.findById(storagePoolHostVO.getHostId()); | ||
| return hostVO.getGuid(); | ||
| } | ||
|
|
||
| private String getHostGuidInTargetCluster(boolean isInterClusterMigration, Long destClusterId) { | ||
| String hostGuidInTargetCluster = null; | ||
| if (isInterClusterMigration) { | ||
|
|
@@ -1096,6 +1106,7 @@ public List<Command> finalizeMigrate(VirtualMachine vm, Map<Volume, StoragePool> | |
| // OfflineVmwareMigration: specialised migration command | ||
| List<Pair<VolumeTO, StorageFilerTO>> volumeToFilerTo = new ArrayList<Pair<VolumeTO, StorageFilerTO>>(); | ||
| Long poolClusterId = null; | ||
| StoragePool targetLocalPoolForVM = null; | ||
| for (Map.Entry<Volume, StoragePool> entry : volumeToPool.entrySet()) { | ||
| Volume volume = entry.getKey(); | ||
| StoragePool pool = entry.getValue(); | ||
|
|
@@ -1104,13 +1115,18 @@ public List<Command> finalizeMigrate(VirtualMachine vm, Map<Volume, StoragePool> | |
| if (pool.getClusterId() != null) { | ||
| poolClusterId = pool.getClusterId(); | ||
| } | ||
| if (volume.getVolumeType().equals(Volume.Type.ROOT) && pool.isLocal()) { | ||
| targetLocalPoolForVM = pool; | ||
| } | ||
| volumeToFilerTo.add(new Pair<VolumeTO, StorageFilerTO>(volumeTo, filerTo)); | ||
| } | ||
| final Long destClusterId = poolClusterId; | ||
| final Long srcClusterId = vmManager.findClusterAndHostIdForVm(vm.getId()).first(); | ||
| final boolean isInterClusterMigration = isInterClusterMigration(destClusterId, srcClusterId); | ||
| String targetHostGuid = getTargetHostGuid(targetLocalPoolForVM, destClusterId, isInterClusterMigration); | ||
|
|
||
| MigrateVmToPoolCommand migrateVmToPoolCommand = new MigrateVmToPoolCommand(vm.getInstanceName(), | ||
| volumeToFilerTo, getHostGuidInTargetCluster(isInterClusterMigration, destClusterId), true); | ||
| volumeToFilerTo, targetHostGuid, true); | ||
| commands.add(migrateVmToPoolCommand); | ||
|
|
||
| // OfflineVmwareMigration: cleanup if needed | ||
|
|
@@ -1127,6 +1143,17 @@ public List<Command> finalizeMigrate(VirtualMachine vm, Map<Volume, StoragePool> | |
| return commands; | ||
| } | ||
|
|
||
| private String getTargetHostGuid(StoragePool targetLocalPoolForVM, Long destClusterId, boolean isInterClusterMigration) { | ||
| String targetHostGuid = null; | ||
| if (targetLocalPoolForVM != null) { | ||
| // Get the target host for local storage migration | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 😁 this comment says kind of exactly what the code line below says as well. not sure we need it, but hey ... |
||
| targetHostGuid = getHostGuidForLocalStorage(targetLocalPoolForVM); | ||
| } else { | ||
| targetHostGuid = getHostGuidInTargetCluster(isInterClusterMigration, destClusterId); | ||
| } | ||
| return targetHostGuid; | ||
| } | ||
|
|
||
| @Override | ||
| protected VirtualMachineTO toVirtualMachineTO(VirtualMachineProfile vmProfile) { | ||
| return super.toVirtualMachineTO(vmProfile); | ||
|
|
||
119 changes: 119 additions & 0 deletions
119
plugins/hypervisors/vmware/src/test/java/com/cloud/hypervisor/guru/VMwareGuruTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
| package com.cloud.hypervisor.guru; | ||
|
|
||
| import com.cloud.agent.api.Command; | ||
| import com.cloud.agent.api.MigrateVmToPoolCommand; | ||
| import com.cloud.dc.ClusterDetailsDao; | ||
| import com.cloud.host.HostVO; | ||
| import com.cloud.host.dao.HostDao; | ||
| import com.cloud.storage.StoragePool; | ||
| import com.cloud.storage.StoragePoolHostVO; | ||
| import com.cloud.storage.Volume; | ||
| import com.cloud.storage.dao.StoragePoolHostDao; | ||
| import com.cloud.utils.Pair; | ||
| import com.cloud.vm.VirtualMachine; | ||
| import com.cloud.vm.VirtualMachineManager; | ||
| import org.apache.cloudstack.storage.datastore.db.PrimaryDataStoreDao; | ||
| import org.apache.cloudstack.storage.datastore.db.StoragePoolVO; | ||
| import org.junit.Assert; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
| import org.mockito.InjectMocks; | ||
| import org.mockito.Mock; | ||
| import org.mockito.Mockito; | ||
| import org.mockito.MockitoAnnotations; | ||
| import org.mockito.Spy; | ||
| import org.powermock.core.classloader.annotations.PrepareForTest; | ||
| import org.powermock.modules.junit4.PowerMockRunner; | ||
| import org.springframework.test.context.ContextConfiguration; | ||
| import org.springframework.test.context.support.AnnotationConfigContextLoader; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| @RunWith(PowerMockRunner.class) | ||
| @PrepareForTest({VMwareGuru.class}) | ||
| @ContextConfiguration(loader = AnnotationConfigContextLoader.class) | ||
| public class VMwareGuruTest { | ||
|
|
||
| @Spy | ||
| @InjectMocks | ||
| private VMwareGuru vMwareGuru = new VMwareGuru(); | ||
|
|
||
| @Mock | ||
| PrimaryDataStoreDao _storagePoolDao; | ||
|
|
||
| @Mock | ||
| StoragePoolHostDao storagePoolHostDao; | ||
|
|
||
| @Mock | ||
| HostDao _hostDao; | ||
|
|
||
| @Mock | ||
| VirtualMachineManager vmManager; | ||
|
|
||
| @Mock | ||
| ClusterDetailsDao _clusterDetailsDao; | ||
|
|
||
| @Before | ||
| public void testSetUp() throws Exception { | ||
| MockitoAnnotations.initMocks(this); | ||
| } | ||
|
|
||
| @Test | ||
| public void finalizeMigrateForLocalStorageToHaveTargetHostGuid(){ | ||
| VirtualMachine vm = Mockito.mock(VirtualMachine.class); | ||
| Map<Volume, StoragePool> volumeToPool = new HashMap<>(); | ||
| Volume rootVolume = Mockito.mock(Volume.class); | ||
| Volume dataVolume = Mockito.mock(Volume.class); | ||
| StoragePool localStorage = Mockito.mock(StoragePool.class); | ||
| volumeToPool.put(rootVolume, localStorage); | ||
| volumeToPool.put(dataVolume, localStorage); | ||
|
|
||
| // prepare localstorage host guid | ||
| StoragePoolVO storagePoolVO = Mockito.mock(StoragePoolVO.class); | ||
| StoragePoolHostVO storagePoolHostVO = Mockito.mock(StoragePoolHostVO.class); | ||
| HostVO hostVO = Mockito.mock(HostVO.class); | ||
|
|
||
| Mockito.when(localStorage.getId()).thenReturn(1L); | ||
| Mockito.when(vm.getId()).thenReturn(1L); | ||
| Mockito.when(_storagePoolDao.findById(1L)).thenReturn(storagePoolVO); | ||
| Mockito.when(rootVolume.getVolumeType()).thenReturn(Volume.Type.ROOT); | ||
| Mockito.when(dataVolume.getVolumeType()).thenReturn(Volume.Type.DATADISK); | ||
| Mockito.when(localStorage.isLocal()).thenReturn(true); | ||
| Pair<Long, Long> clusterAndHost = new Pair<>(1L, 1L); | ||
|
|
||
| Mockito.when(vmManager.findClusterAndHostIdForVm(1L)).thenReturn(clusterAndHost); | ||
|
|
||
| List<StoragePoolHostVO> storagePoolHostVOS = new ArrayList<>(); | ||
| storagePoolHostVOS.add(storagePoolHostVO); | ||
| Mockito.when(storagePoolHostDao.listByPoolId(1L)).thenReturn(storagePoolHostVOS); | ||
| Mockito.when(storagePoolHostVO.getHostId()).thenReturn(2L); | ||
| Mockito.when(_hostDao.findById(2L)).thenReturn(hostVO); | ||
| Mockito.when(hostVO.getGuid()).thenReturn("HostSystem:host-a@x.x.x.x"); | ||
|
|
||
| List<Command> commandsList = vMwareGuru.finalizeMigrate(vm, volumeToPool); | ||
|
|
||
| MigrateVmToPoolCommand migrateVmToPoolCommand = (MigrateVmToPoolCommand) commandsList.get(0); | ||
| Assert.assertEquals("HostSystem:host-a@x.x.x.x", migrateVmToPoolCommand.getHostGuidInTargetCluster()); | ||
| } | ||
|
|
||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.