Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,15 @@
import org.apache.cloudstack.storage.datastore.db.StoragePoolVO;
import org.apache.cloudstack.storage.to.PrimaryDataStoreTO;
import org.apache.cloudstack.storage.to.VolumeObjectTO;
import org.apache.cloudstack.storage.volume.VolumeOnStorageTO;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;

import com.cloud.agent.AgentManager;
import com.cloud.agent.api.Answer;
import com.cloud.agent.api.GetVolumesOnStorageAnswer;
import com.cloud.agent.api.GetVolumesOnStorageCommand;
import com.cloud.agent.api.MigrateAnswer;
import com.cloud.agent.api.MigrateCommand;
import com.cloud.agent.api.MigrateCommand.MigrateDiskInfo;
Expand All @@ -96,6 +99,7 @@
import com.cloud.agent.api.to.DataTO;
import com.cloud.agent.api.to.DiskTO;
import com.cloud.agent.api.to.NfsTO;
import com.cloud.agent.api.to.StorageFilerTO;
import com.cloud.agent.api.to.VirtualMachineTO;
import com.cloud.configuration.Config;
import com.cloud.dc.dao.ClusterDao;
Expand Down Expand Up @@ -2056,7 +2060,7 @@ public void copyAsync(Map<VolumeInfo, DataStore> volumeDataStoreMap, VirtualMach
continue;
}

MigrationOptions.Type migrationType = decideMigrationTypeAndCopyTemplateIfNeeded(destHost, vmInstance, srcVolumeInfo, sourceStoragePool, destStoragePool, destDataStore);
MigrationOptions.Type migrationType = decideMigrationTypeAndCopyTemplateIfNeeded(srcHost, destHost, vmInstance, srcVolumeInfo, sourceStoragePool, destStoragePool, destDataStore);
migrateNonSharedInc = migrateNonSharedInc || MigrationOptions.Type.LinkedClone.equals(migrationType);

VolumeVO destVolume = duplicateVolumeOnAnotherStorage(srcVolume, destStoragePool);
Expand Down Expand Up @@ -2211,13 +2215,14 @@ public void copyAsync(Map<VolumeInfo, DataStore> volumeDataStoreMap, VirtualMach
}
}

private MigrationOptions.Type decideMigrationTypeAndCopyTemplateIfNeeded(Host destHost, VMInstanceVO vmInstance, VolumeInfo srcVolumeInfo, StoragePoolVO sourceStoragePool, StoragePoolVO destStoragePool, DataStore destDataStore) {
private MigrationOptions.Type decideMigrationTypeAndCopyTemplateIfNeeded(Host srcHost, Host destHost, VMInstanceVO vmInstance, VolumeInfo srcVolumeInfo, StoragePoolVO sourceStoragePool, StoragePoolVO destStoragePool, DataStore destDataStore) {
VMTemplateVO vmTemplate = _vmTemplateDao.findById(vmInstance.getTemplateId());
String srcVolumeBackingFile = getVolumeBackingFile(srcVolumeInfo);
if (StringUtils.isNotBlank(srcVolumeBackingFile) && supportStoragePoolType(destStoragePool.getPoolType(), StoragePoolType.Filesystem) &&
srcVolumeInfo.getTemplateId() != null &&
Objects.nonNull(vmTemplate) &&
!Arrays.asList(KVM_VM_IMPORT_DEFAULT_TEMPLATE_NAME, VM_IMPORT_DEFAULT_TEMPLATE_NAME).contains(vmTemplate.getName())) {
!Arrays.asList(KVM_VM_IMPORT_DEFAULT_TEMPLATE_NAME, VM_IMPORT_DEFAULT_TEMPLATE_NAME).contains(vmTemplate.getName()) &&
hasBackingFileOnSourceHost(srcHost, srcVolumeInfo, sourceStoragePool)) {
logger.debug(String.format("Copying template [%s] of volume [%s] from source storage pool [%s] to target storage pool [%s].", srcVolumeInfo.getTemplateId(), srcVolumeInfo.getId(), sourceStoragePool.getId(), destStoragePool.getId()));
copyTemplateToTargetFilesystemStorageIfNeeded(srcVolumeInfo, sourceStoragePool, destDataStore, destStoragePool, destHost);
return MigrationOptions.Type.LinkedClone;
Expand All @@ -2227,6 +2232,34 @@ private MigrationOptions.Type decideMigrationTypeAndCopyTemplateIfNeeded(Host de
return MigrationOptions.Type.FullClone;
}

/**
* Checks on the source host if the file of the volume has a backing file. The template reference of the storage pool only tells that the template is cached there; it
* does not tell that the volume still depends on it. A volume that was flattened (for instance, by an offline migration to another storage pool) or that was created as
* a full clone has no backing file, and migrating it as a linked clone would make the template data show through every region that is not allocated in the volume file.
* Returns true without asking the host if the source storage pool does not keep volumes as files.
*/
protected boolean hasBackingFileOnSourceHost(Host srcHost, VolumeInfo srcVolumeInfo, StoragePoolVO sourceStoragePool) {
if (!supportStoragePoolType(sourceStoragePool.getPoolType(), StoragePoolType.Filesystem)) {
return true;
}

GetVolumesOnStorageCommand command = new GetVolumesOnStorageCommand(new StorageFilerTO(sourceStoragePool), srcVolumeInfo.getPath(), null);
Answer answer = agentManager.easySend(srcHost.getId(), command);
if (!(answer instanceof GetVolumesOnStorageAnswer) || !answer.getResult() || CollectionUtils.isEmpty(((GetVolumesOnStorageAnswer) answer).getVolumes())) {
String reason = answer != null ? answer.getDetails() : "no answer from the host";
throw new CloudRuntimeException(String.format("Unable to check on host [%s] if volume [%s] has a backing file, which is required to migrate it safely: %s",
srcHost.getUuid(), srcVolumeInfo.getUuid(), reason));
}

Map<VolumeOnStorageTO.Detail, String> details = ((GetVolumesOnStorageAnswer) answer).getVolumes().get(0).getDetails();
boolean hasBackingFile = details != null && StringUtils.isNotBlank(details.get(VolumeOnStorageTO.Detail.BACKING_FILE));
if (!hasBackingFile) {
logger.info("Volume [{}] has template [{}] cached on storage pool [{}], but its file has no backing file on host [{}]; it will be migrated as a full clone.",
srcVolumeInfo.getUuid(), srcVolumeInfo.getTemplateId(), sourceStoragePool.getUuid(), srcHost.getUuid());
}
return hasBackingFile;
}

protected String formatMigrationElementsAsJsonToDisplayOnLog(String objectName, Object object, Object from, Object to){
return String.format("{%s: \"%s\", from: \"%s\", to:\"%s\"}", objectName, object, from, to);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import static org.mockito.Mockito.mock;
import static org.mockito.MockitoAnnotations.initMocks;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -38,6 +39,7 @@
import org.apache.cloudstack.storage.datastore.db.StoragePoolVO;
import org.apache.cloudstack.storage.image.store.ImageStoreImpl;
import org.apache.cloudstack.storage.volume.VolumeObject;
import org.apache.cloudstack.storage.volume.VolumeOnStorageTO;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -48,6 +50,10 @@
import org.mockito.Spy;
import org.mockito.junit.MockitoJUnitRunner;

import com.cloud.agent.AgentManager;
import com.cloud.agent.api.Answer;
import com.cloud.agent.api.GetVolumesOnStorageAnswer;
import com.cloud.agent.api.GetVolumesOnStorageCommand;
import com.cloud.agent.api.MigrateCommand;
import com.cloud.host.HostVO;
import com.cloud.storage.DataStoreRole;
Expand All @@ -57,6 +63,7 @@
import com.cloud.storage.Storage.StoragePoolType;
import com.cloud.storage.Volume;
import com.cloud.storage.VolumeVO;
import com.cloud.utils.exception.CloudRuntimeException;
import java.util.AbstractMap;
import java.util.Arrays;
import java.util.HashSet;
Expand All @@ -80,6 +87,8 @@ public class StorageSystemDataMotionStrategyTest {
private ImageStore destinationStore;
@Mock
private PrimaryDataStoreDao primaryDataStoreDao;
@Mock
private AgentManager agentManager;

@Mock
StoragePoolVO sourceStoragePoolVoMock, destinationStoragePoolVoMock;
Expand Down Expand Up @@ -369,4 +378,63 @@ public void validateIsStoragePoolTypeInListReturnsFalse() {

assertFalse(strategy.isStoragePoolTypeInList(StoragePoolType.SharedMountPoint, listTypes));
}

private GetVolumesOnStorageAnswer mockAnswerWithVolume(String backingFile) {
VolumeOnStorageTO volumeOnStorage = new VolumeOnStorageTO();
if (backingFile != null) {
volumeOnStorage.addDetail(VolumeOnStorageTO.Detail.BACKING_FILE, backingFile);
}
GetVolumesOnStorageAnswer answer = mock(GetVolumesOnStorageAnswer.class);
doReturn(true).when(answer).getResult();
doReturn(Collections.singletonList(volumeOnStorage)).when(answer).getVolumes();
return answer;
}

private boolean hasBackingFileOnSourceHost(StoragePoolType sourcePoolType, Answer answer) {
doReturn(sourcePoolType).when(sourceStoragePoolVoMock).getPoolType();
lenient().doReturn(answer).when(agentManager).easySend(Mockito.anyLong(), Mockito.any(GetVolumesOnStorageCommand.class));
return strategy.hasBackingFileOnSourceHost(mock(HostVO.class), mock(VolumeObject.class), sourceStoragePoolVoMock);
}

@Test
public void hasBackingFileOnSourceHostTestVolumeWithBackingFile() {
assertTrue(hasBackingFileOnSourceHost(StoragePoolType.NetworkFilesystem, mockAnswerWithVolume("/mnt/pool/template")));
}

@Test
public void hasBackingFileOnSourceHostTestVolumeWithoutBackingFile() {
assertFalse(hasBackingFileOnSourceHost(StoragePoolType.NetworkFilesystem, mockAnswerWithVolume(null)));
}

@Test
public void hasBackingFileOnSourceHostTestLocalAndSharedMountPointPoolsAreChecked() {
assertFalse(hasBackingFileOnSourceHost(StoragePoolType.Filesystem, mockAnswerWithVolume(null)));
assertFalse(hasBackingFileOnSourceHost(StoragePoolType.SharedMountPoint, mockAnswerWithVolume(null)));
}

@Test
public void hasBackingFileOnSourceHostTestPoolWithoutVolumeFilesIsNotChecked() {
assertTrue(hasBackingFileOnSourceHost(StoragePoolType.RBD, null));
Mockito.verify(agentManager, Mockito.never()).easySend(Mockito.anyLong(), Mockito.any());
}

@Test(expected = CloudRuntimeException.class)
public void hasBackingFileOnSourceHostTestNoAnswerFromHost() {
hasBackingFileOnSourceHost(StoragePoolType.NetworkFilesystem, null);
}

@Test(expected = CloudRuntimeException.class)
public void hasBackingFileOnSourceHostTestFailedAnswer() {
GetVolumesOnStorageAnswer answer = mock(GetVolumesOnStorageAnswer.class);
doReturn(false).when(answer).getResult();
hasBackingFileOnSourceHost(StoragePoolType.NetworkFilesystem, answer);
}

@Test(expected = CloudRuntimeException.class)
public void hasBackingFileOnSourceHostTestVolumeNotFoundOnHost() {
GetVolumesOnStorageAnswer answer = mock(GetVolumesOnStorageAnswer.class);
doReturn(true).when(answer).getResult();
doReturn(Collections.emptyList()).when(answer).getVolumes();
hasBackingFileOnSourceHost(StoragePoolType.NetworkFilesystem, answer);
}
}