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
4 changes: 4 additions & 0 deletions api/src/main/java/com/cloud/host/Host.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ public static String[] toStrings(Host.Type... types) {

String HOST_UEFI_ENABLE = "host.uefi.enable";
String HOST_KVM_DISK_ONLY_VM_SNAPSHOT_NVRAM = "host.kvm.diskonlyvmsnapshot.nvram";
/**
* The KVM agent excludes the disks that are not being snapshotted from disk-only snapshots of running Instances, so a single volume can be snapshotted.
*/
String HOST_KVM_DISK_ONLY_SNAPSHOT_SINGLE_VOLUME = "host.kvm.diskonlysnapshot.singlevolume";
String HOST_VOLUME_ENCRYPTION = "host.volume.encryption";
String HOST_RBD_VOLUME_ENCRYPTION = "host.volume.encryption.rbd";
String HOST_INSTANCE_CONVERSION = "host.instance.conversion";
Expand Down
5 changes: 5 additions & 0 deletions api/src/main/java/com/cloud/storage/Snapshot.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
import java.util.Date;

public interface Snapshot extends ControlledEntity, Identity, InternalIdentity, StateObject<Snapshot.State> {
/**
* Name of the snapshot detail that marks a KVM file-based no-copy volume snapshot, which is part of the backing chain of its volume.
*/
String KVM_FILE_BASED_NO_COPY_SNAPSHOT_DETAIL = "kvmFileBasedNoCopySnapshot";

public enum Type {
MANUAL, RECURRING, TEMPLATE, HOURLY, DAILY, WEEKLY, MONTHLY, GROUP, FROM_GROUP;
private int max = 8;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,7 @@ protected AgentAttache notifyMonitorsOfConnection(final AgentAttache attache, fi
}
String uefiEnabled = detailsMap.get(Host.HOST_UEFI_ENABLE);
String diskOnlyVmSnapshotNvramSupport = detailsMap.get(Host.HOST_KVM_DISK_ONLY_VM_SNAPSHOT_NVRAM);
String diskOnlySnapshotSingleVolumeSupport = detailsMap.get(Host.HOST_KVM_DISK_ONLY_SNAPSHOT_SINGLE_VOLUME);
String virtv2vVersion = detailsMap.get(Host.HOST_VIRTV2V_VERSION);
String ovftoolVersion = detailsMap.get(Host.HOST_OVFTOOL_VERSION);
String vddkSupport = detailsMap.get(Host.HOST_VDDK_SUPPORT);
Expand All @@ -831,7 +832,7 @@ protected AgentAttache notifyMonitorsOfConnection(final AgentAttache attache, fi
String qemuImgRbdSupport = detailsMap.get(Host.HOST_QEMU_RBD_SUPPORT);
String vddkRbdDirectImportSupport = detailsMap.get(Host.HOST_VDDK_RBD_DIRECT_IMPORT_SUPPORT);
logger.debug("Got HOST_UEFI_ENABLE [{}] for host [{}]:", uefiEnabled, host);
if (ObjectUtils.anyNotNull(uefiEnabled, diskOnlyVmSnapshotNvramSupport, virtv2vVersion, ovftoolVersion, vddkSupport, vddkLibDir, vddkVersion,
if (ObjectUtils.anyNotNull(uefiEnabled, diskOnlyVmSnapshotNvramSupport, diskOnlySnapshotSingleVolumeSupport, virtv2vVersion, ovftoolVersion, vddkSupport, vddkLibDir, vddkVersion,
vlanFilteringEnabled, guestBridgeStpEnabled, vlanTrunkXmlSupported,
vmwareCbtSupport, vmwareCbtInPlaceFinalizationSupport, vmwareCbtRbdSupport, qemuImgVersion, qemuNbdVersion, qemuIoVersion, virtV2vInPlaceVersion,
virtv2vInPlaceSupport, qemuImgRbdSupport, vddkRbdDirectImportSupport)) {
Expand All @@ -843,6 +844,9 @@ protected AgentAttache notifyMonitorsOfConnection(final AgentAttache attache, fi
if (syncBooleanHostCapability(host, Host.HOST_KVM_DISK_ONLY_VM_SNAPSHOT_NVRAM, diskOnlyVmSnapshotNvramSupport)) {
updateNeeded = true;
}
if (syncBooleanHostCapability(host, Host.HOST_KVM_DISK_ONLY_SNAPSHOT_SINGLE_VOLUME, diskOnlySnapshotSingleVolumeSupport)) {
updateNeeded = true;
}
if (StringUtils.isNotBlank(virtv2vVersion) && !virtv2vVersion.equals(host.getDetails().get(Host.HOST_VIRTV2V_VERSION))) {
host.getDetails().put(Host.HOST_VIRTV2V_VERSION, virtv2vVersion);
updateNeeded = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@
import com.cloud.storage.VolumeVO;
import com.cloud.storage.dao.DiskOfferingDao;
import com.cloud.storage.dao.SnapshotDao;
import com.cloud.storage.dao.SnapshotDetailsDao;
import com.cloud.storage.dao.StoragePoolHostDao;
import com.cloud.storage.dao.VMTemplateDetailsDao;
import com.cloud.storage.dao.VolumeDao;
Expand Down Expand Up @@ -228,6 +229,8 @@ public enum UserVmCloneType {
@Inject
protected SnapshotDao _snapshotDao;
@Inject
protected SnapshotDetailsDao snapshotDetailsDao;
@Inject
protected SnapshotDataStoreDao _snapshotDataStoreDao;
@Inject
protected ResourceLimitService _resourceLimitMgr;
Expand Down Expand Up @@ -1887,6 +1890,19 @@ private static class VolumeTask {
}
}

/**
* A KVM file-based no-copy snapshot operation that is not serialized with the Instance (deleting or reverting a snapshot of a stopped Instance) may be
* replacing or merging the files of the volume: starting the Instance meanwhile would open files that are about to be removed.
*/
protected void checkVolumeIsNotUnderSnapshotOperation(VolumeVO vol) {
if ((Volume.State.Snapshotting.equals(vol.getState()) || Volume.State.RevertSnapshotting.equals(vol.getState())) && volumeHasKvmFileBasedNoCopySnapshots(vol.getId())) {
throw new CloudRuntimeException(String.format("Volume %s is in state %s: the Instance cannot be started until the snapshot operation on it finishes. If " +
"no operation is running, a KVM file-based no-copy snapshot operation on it did not complete: check the error of that operation if there is one, " +
"otherwise the details of the volume's snapshots and its backing chain on the storage; set the paths and the volume state as needed, and start " +
"the Instance again.", getReflectOnlySelectedFields(vol), vol.getState()));
}
}

private List<VolumeTask> getTasks(List<VolumeVO> vols, Map<Volume, StoragePool> destVols, VirtualMachineProfile vm) throws StorageUnavailableException {
boolean recreate = RecreatableSystemVmEnabled.value();
List<VolumeTask> tasks = new ArrayList<VolumeTask>();
Expand Down Expand Up @@ -2290,6 +2306,12 @@ public void prepare(VirtualMachineProfile vm, DeployDestination dest) throws Sto
throw new CloudRuntimeException(String.format("ROOT volume is missing, unable to prepare volumes for the VM [%s].", vm.getVirtualMachine()));
}

// Checked on all the volumes of the Instance: the usable volumes below leave out the volumes reserved by a snapshot operation, so the Instance would
// otherwise start without them.
for (VolumeVO vol : _volsDao.findByInstance(vm.getId())) {
checkVolumeIsNotUnderSnapshotOperation(vol);
}

List<VolumeVO> vols = _volsDao.findUsableVolumesForInstance(vm.getId());

VirtualMachine vmInstance = vm.getVirtualMachine();
Expand Down Expand Up @@ -2513,12 +2535,35 @@ private void cleanupVolumeDuringMigrationFailure(Long volumeId, Long destPoolId)

}

private void cleanupVolumeDuringSnapshotFailure(Long volumeId, Long snapshotId) {
/**
* @return true if the volume has KVM file-based no-copy snapshots (including one being taken), which are part of its backing chain.
*/
protected boolean volumeHasKvmFileBasedNoCopySnapshots(long volumeId) {
return _snapshotDao.listByVolumeId(volumeId).stream()
.filter(snapshot -> !Snapshot.State.Destroyed.equals(snapshot.getState()) && !Snapshot.State.Error.equals(snapshot.getState()))
.anyMatch(snapshot -> snapshotDetailsDao.findDetail(snapshot.getId(), Snapshot.KVM_FILE_BASED_NO_COPY_SNAPSHOT_DETAIL) != null);
}

protected void cleanupVolumeDuringSnapshotFailure(Long volumeId, Long snapshotId) {
if (snapshotId != null && snapshotDetailsDao.findDetail(snapshotId, Snapshot.KVM_FILE_BASED_NO_COPY_SNAPSHOT_DETAIL) != null) {
// The outcome of a KVM file-based no-copy snapshot take is unknown: a running Instance may already be writing to the new file. Resetting the volume
// with its old path would make the next start lose those writes, so it is left for an administrator (see the snapshot details).
logger.warn("Not cleaning up volume [{}] after the failed KVM file-based no-copy snapshot [{}]: check which file of its backing chain is current " +
"(snapshot details [kvmFileBasedNoCopySnapshotPendingVolumePath] and [kvmFileBasedNoCopySnapshotPendingFrozenPath]), set the volume path and the " +
"volume state to Ready, then delete the snapshot: that completes or discards it, depending on the volume path.", volumeId, snapshotId);
return;
}
_snapshotSrv.cleanupVolumeDuringSnapshotFailure(volumeId, snapshotId);
VolumeVO volume = _volsDao.findById(volumeId);

String volumeToString = getReflectOnlySelectedFields(volume);

if (volume.getState() == Volume.State.Snapshotting && volumeHasKvmFileBasedNoCopySnapshots(volumeId)) {
// Another operation on its KVM file-based no-copy snapshots (a take or a delete without an answer) may hold the volume deliberately.
logger.warn("Not changing the state of volume [{}] back to Ready, as it has KVM file-based no-copy snapshots: check them, then set its state.",
volumeToString);
return;
}
if (volume.getState() == Volume.State.Snapshotting) {
logger.debug("Changing volume [{}] state back to Ready.", volumeToString);
volume.setState(Volume.State.Ready);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,26 @@ public void testNotifyMonitorsOfConnectionClearsStaleNvramCapabilityOnReconnect(
Mockito.verify(hostDao).saveDetails(host);
}

@Test
public void testNotifyMonitorsOfConnectionStoresSingleVolumeDiskOnlySnapshotCapability() throws ConnectionException {
ReadyAnswer readyAnswer = Mockito.mock(ReadyAnswer.class);
host.setDetails(new HashMap<>());

Mockito.when(hostDao.findById(Mockito.anyLong())).thenReturn(host);
Mockito.doNothing().when(hostDao).loadDetails(host);
Mockito.doNothing().when(storagePoolMonitor).processConnect(Mockito.eq(host), Mockito.eq(cmds[0]), Mockito.eq(false));
Mockito.when(readyAnswer.getResult()).thenReturn(true);
Mockito.when(readyAnswer.getDetailsMap()).thenReturn(Map.of(Host.HOST_KVM_DISK_ONLY_SNAPSHOT_SINGLE_VOLUME, Boolean.TRUE.toString()));
Mockito.doReturn(readyAnswer).when(mgr).easySend(Mockito.anyLong(), Mockito.any(ReadyCommand.class));
Mockito.doReturn(true).when(mgr).agentStatusTransitTo(Mockito.eq(host), Mockito.eq(Status.Event.Ready), Mockito.anyLong());

final AgentAttache agentAttache = mgr.notifyMonitorsOfConnection(attache, cmds, false);

Assert.assertTrue(agentAttache.isReady());
Assert.assertEquals(Boolean.TRUE.toString(), host.getDetails().get(Host.HOST_KVM_DISK_ONLY_SNAPSHOT_SINGLE_VOLUME));
Mockito.verify(hostDao).saveDetails(host);
}

@Test
public void testGetTimeoutWithPositiveTimeout() {
Commands commands = Mockito.mock(Commands.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import com.cloud.configuration.Resource;
import com.cloud.deploy.DeploymentClusterPlanner;
import com.cloud.deploy.DeployDestination;
import com.cloud.exception.InvalidParameterValueException;
import com.cloud.exception.StorageAccessException;
import com.cloud.host.Host;
Expand All @@ -47,6 +48,7 @@
import com.cloud.utils.exception.CloudRuntimeException;
import com.cloud.vm.VMInstanceVO;
import com.cloud.vm.VirtualMachine;
import com.cloud.vm.VirtualMachineProfile;
import com.cloud.utils.Pair;
import org.apache.cloudstack.engine.subsystem.api.storage.DataObject;
import org.apache.cloudstack.engine.subsystem.api.storage.DataStore;
Expand All @@ -58,6 +60,12 @@
import org.apache.cloudstack.storage.datastore.db.ImageStoreDao;
import org.apache.cloudstack.storage.datastore.db.ImageStoreVO;
import org.apache.cloudstack.storage.datastore.db.SnapshotDataStoreDao;
import com.cloud.storage.Snapshot;
import com.cloud.storage.SnapshotVO;
import com.cloud.storage.dao.SnapshotDetailsVO;
import com.cloud.storage.dao.SnapshotDao;
import com.cloud.storage.dao.SnapshotDetailsDao;
import org.apache.cloudstack.engine.subsystem.api.storage.SnapshotService;
import org.apache.cloudstack.storage.datastore.db.SnapshotDataStoreVO;
import org.apache.cloudstack.framework.config.ConfigDepot;
import org.apache.cloudstack.framework.config.ConfigKey;
Expand Down Expand Up @@ -970,4 +978,111 @@ private Field findField(Class<?> clazz, String fieldName) {
return null;
}

@Test
public void checkVolumeIsNotUnderSnapshotOperationRefusesReservedVolumesWithKvmFileBasedNoCopySnapshots() {
Mockito.doReturn(true).when(volumeOrchestrator).volumeHasKvmFileBasedNoCopySnapshots(Mockito.anyLong());
for (Volume.State state : List.of(Volume.State.Snapshotting, Volume.State.RevertSnapshotting)) {
VolumeVO volume = Mockito.mock(VolumeVO.class);
Mockito.when(volume.getState()).thenReturn(state);
Assert.assertThrows(CloudRuntimeException.class, () -> volumeOrchestrator.checkVolumeIsNotUnderSnapshotOperation(volume));
}
}

@Test
public void checkVolumeIsNotUnderSnapshotOperationAllowsReservedVolumesWithoutKvmFileBasedNoCopySnapshots() {
Mockito.doReturn(false).when(volumeOrchestrator).volumeHasKvmFileBasedNoCopySnapshots(Mockito.anyLong());
for (Volume.State state : List.of(Volume.State.Snapshotting, Volume.State.RevertSnapshotting)) {
VolumeVO volume = Mockito.mock(VolumeVO.class);
Mockito.when(volume.getState()).thenReturn(state);
volumeOrchestrator.checkVolumeIsNotUnderSnapshotOperation(volume);
}
}

@Test
public void prepareRefusesToStartAnInstanceWithAVolumeReservedByANoCopySnapshotOperation() {
VirtualMachineProfile profile = Mockito.mock(VirtualMachineProfile.class);
Mockito.when(profile.getId()).thenReturn(MOCK_VM_ID);
VolumeVO root = Mockito.mock(VolumeVO.class);
Mockito.when(volumeDao.findByInstanceAndType(MOCK_VM_ID, Volume.Type.ROOT)).thenReturn(List.of(root));
VolumeVO reserved = Mockito.mock(VolumeVO.class);
Mockito.when(reserved.getId()).thenReturn(7L);
Mockito.when(reserved.getState()).thenReturn(Volume.State.Snapshotting);
Mockito.when(volumeDao.findByInstance(MOCK_VM_ID)).thenReturn(List.of(reserved));
Mockito.doReturn(true).when(volumeOrchestrator).volumeHasKvmFileBasedNoCopySnapshots(7L);

Assert.assertThrows(CloudRuntimeException.class, () -> volumeOrchestrator.prepare(profile, Mockito.mock(DeployDestination.class)));

// The usable volumes leave the reserved volume out, so it must be checked before they are listed.
Mockito.verify(volumeDao, Mockito.never()).findUsableVolumesForInstance(Mockito.anyLong());
}

@Test
public void volumeHasKvmFileBasedNoCopySnapshotsIgnoresDestroyedAndErrorSnapshots() {
SnapshotDao snapshotDao = Mockito.mock(SnapshotDao.class);
SnapshotDetailsDao snapshotDetailsDao = Mockito.mock(SnapshotDetailsDao.class);
volumeOrchestrator._snapshotDao = snapshotDao;
volumeOrchestrator.snapshotDetailsDao = snapshotDetailsDao;
List<SnapshotVO> snapshots = new ArrayList<>();
long id = 1;
for (Snapshot.State state : List.of(Snapshot.State.Destroyed, Snapshot.State.Error, Snapshot.State.BackedUp)) {
SnapshotVO snapshot = Mockito.mock(SnapshotVO.class);
Mockito.when(snapshot.getState()).thenReturn(state);
Mockito.lenient().when(snapshot.getId()).thenReturn(id++);
snapshots.add(snapshot);
}
Mockito.when(snapshotDao.listByVolumeId(1L)).thenReturn(snapshots);

Assert.assertFalse(volumeOrchestrator.volumeHasKvmFileBasedNoCopySnapshots(1L));

Mockito.when(snapshotDetailsDao.findDetail(3L, Snapshot.KVM_FILE_BASED_NO_COPY_SNAPSHOT_DETAIL)).thenReturn(Mockito.mock(SnapshotDetailsVO.class));
Assert.assertTrue(volumeOrchestrator.volumeHasKvmFileBasedNoCopySnapshots(1L));
Mockito.verify(snapshotDetailsDao, Mockito.never()).findDetail(1L, Snapshot.KVM_FILE_BASED_NO_COPY_SNAPSHOT_DETAIL);
Mockito.verify(snapshotDetailsDao, Mockito.never()).findDetail(2L, Snapshot.KVM_FILE_BASED_NO_COPY_SNAPSHOT_DETAIL);
}

@Test
public void cleanupVolumeDuringSnapshotFailureLeavesVolumesOfKvmFileBasedNoCopySnapshotsAlone() {
SnapshotDetailsDao snapshotDetailsDao = Mockito.mock(SnapshotDetailsDao.class);
SnapshotService snapshotService = Mockito.mock(SnapshotService.class);
volumeOrchestrator.snapshotDetailsDao = snapshotDetailsDao;
volumeOrchestrator._snapshotSrv = snapshotService;
Mockito.when(snapshotDetailsDao.findDetail(2L, Snapshot.KVM_FILE_BASED_NO_COPY_SNAPSHOT_DETAIL)).thenReturn(Mockito.mock(SnapshotDetailsVO.class));

volumeOrchestrator.cleanupVolumeDuringSnapshotFailure(1L, 2L);
Mockito.verify(snapshotService, Mockito.never()).cleanupVolumeDuringSnapshotFailure(Mockito.anyLong(), Mockito.anyLong());

VolumeVO volume = Mockito.mock(VolumeVO.class);
Mockito.when(volume.getState()).thenReturn(Volume.State.Ready);
Mockito.doReturn(volume).when(volumeDao).findById(1L);
volumeOrchestrator.cleanupVolumeDuringSnapshotFailure(1L, 3L);
Mockito.verify(snapshotService).cleanupVolumeDuringSnapshotFailure(1L, 3L);
}

@Test
public void cleanupVolumeDuringSnapshotFailureKeepsAVolumeWithKvmFileBasedNoCopySnapshotsReserved() {
SnapshotDetailsDao snapshotDetailsDao = Mockito.mock(SnapshotDetailsDao.class);
SnapshotService snapshotService = Mockito.mock(SnapshotService.class);
volumeOrchestrator.snapshotDetailsDao = snapshotDetailsDao;
volumeOrchestrator._snapshotSrv = snapshotService;
VolumeVO volume = Mockito.mock(VolumeVO.class);
Mockito.when(volume.getState()).thenReturn(Volume.State.Snapshotting);
Mockito.doReturn(volume).when(volumeDao).findById(1L);
// The failed job is of another (regular) snapshot of the volume; a no-copy snapshot operation may hold the volume.
Mockito.doReturn(true).when(volumeOrchestrator).volumeHasKvmFileBasedNoCopySnapshots(1L);

volumeOrchestrator.cleanupVolumeDuringSnapshotFailure(1L, 3L);

Mockito.verify(snapshotService).cleanupVolumeDuringSnapshotFailure(1L, 3L);
Mockito.verify(volume, Mockito.never()).setState(Mockito.any());
Mockito.verify(volumeDao, Mockito.never()).update(Mockito.anyLong(), Mockito.any());
}

@Test
public void checkVolumeIsNotUnderSnapshotOperationAllowsOtherStates() {
for (Volume.State state : List.of(Volume.State.Ready, Volume.State.Allocated, Volume.State.Creating)) {
VolumeVO volume = Mockito.mock(VolumeVO.class);
Mockito.when(volume.getState()).thenReturn(state);
volumeOrchestrator.checkVolumeIsNotUnderSnapshotOperation(volume);
}
}
}
Loading