Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.libvirt.Connect;
Expand Down Expand Up @@ -2928,6 +2929,14 @@ public Answer deleteSnapshot(final DeleteCommand cmd) {
String snapshotName = snapshotFullPath.substring(snapshotFullPath.lastIndexOf("/") + 1);
snapshotFullName = snapshotName;
if (primaryPool.getType() == StoragePoolType.RBD) {
// An RBD snapshot's path is <pool>/<image>/<snapshot>; check the image the snapshot was taken of.
int imageEnd = snapshotFullPath.lastIndexOf("/");
String snapshotImage = imageEnd >= 0 ? snapshotFullPath.substring(0, imageEnd) : volume.getPath();
if (isRbdImageGone(primaryPool, snapshotImage)) {
logger.info("RBD image {} no longer exists, so its snapshot {} was removed with it.", snapshotImage, snapshotName);
return new Answer(cmd, true, "Snapshot " + snapshotName + " was removed with RBD image " + snapshotImage + ".");
}

KVMPhysicalDisk disk = storagePoolMgr.getPhysicalDisk(primaryStore.getPoolType(), primaryStore.getUuid(), volume.getPath());
snapshotFullName = disk.getName() + "@" + snapshotName;
Rados r = radosConnect(primaryPool);
Expand Down Expand Up @@ -2979,6 +2988,22 @@ public Answer deleteSnapshot(final DeleteCommand cmd) {
}
}

/**
* Whether an RBD image no longer exists. Deleting a volume removes its RBD snapshots with the image, so a
* snapshot whose image is gone has nothing left to remove. The image is looked up once, because
* {@link KVMStoragePoolManager#getPhysicalDisk} retries a missing image for minutes, waiting for a volume
* created on another host to appear. Any error other than the image not being found returns false.
*/
protected boolean isRbdImageGone(KVMStoragePool pool, String imageName) {
try {
pool.getPhysicalDisk(imageName);
return false;
} catch (RuntimeException e) {
return ExceptionUtils.getThrowableList(e).stream().anyMatch(t -> t instanceof RbdException
&& ((RbdException) t).getReturnValue() == ErrorCode.ENOENT.getErrorCode());
}
}

/**
* Deletes the checkpoint dump if it exists. And deletes the checkpoint definition on the VM if it is running.
* */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,22 @@

import com.ceph.rados.IoCTX;
import com.ceph.rados.Rados;
import com.ceph.rados.exceptions.RadosException;
import com.ceph.rbd.Rbd;
import com.ceph.rbd.RbdException;
import com.ceph.rbd.RbdImage;
import com.cloud.agent.api.Answer;
import com.cloud.exception.InternalErrorException;
import com.cloud.hypervisor.kvm.resource.LibvirtComputingResource;
import com.cloud.hypervisor.kvm.resource.LibvirtDomainXMLParser;
import com.cloud.hypervisor.kvm.resource.LibvirtVMDef;
import com.cloud.storage.Storage.StoragePoolType;
import com.cloud.storage.template.TemplateConstants;
import com.cloud.utils.Pair;
import com.cloud.utils.exception.CloudRuntimeException;
import com.cloud.utils.script.Script;
import org.apache.cloudstack.storage.command.DeleteCommand;
import org.apache.cloudstack.storage.to.PrimaryDataStoreTO;
import org.apache.cloudstack.storage.to.SnapshotObjectTO;
import org.apache.cloudstack.storage.to.VolumeObjectTO;
import org.apache.cloudstack.utils.qemu.QemuImageOptions;
Expand Down Expand Up @@ -510,6 +515,86 @@ public void getDiskLabelToSnapshotTestDiskMatches() throws LibvirtException {
Assert.assertEquals("vda", result);
}

@Test
public void isRbdImageGoneTestImageExists() {
Mockito.doReturn(Mockito.mock(KVMPhysicalDisk.class)).when(kvmStoragePoolMock).getPhysicalDisk(RBD_IMAGE_NAME);

Assert.assertFalse(storageProcessorSpy.isRbdImageGone(kvmStoragePoolMock, RBD_IMAGE_NAME));
}

@Test
public void isRbdImageGoneTestImageNotFound() {
Mockito.doThrow(new CloudRuntimeException("not found", new RbdException("Failed to open image", -2)))
.when(kvmStoragePoolMock).getPhysicalDisk(RBD_IMAGE_NAME);

Assert.assertTrue(storageProcessorSpy.isRbdImageGone(kvmStoragePoolMock, RBD_IMAGE_NAME));
}

@Test
public void isRbdImageGoneTestOtherRbdError() {
Mockito.doThrow(new CloudRuntimeException("denied", new RbdException("Failed to open image", -13)))
.when(kvmStoragePoolMock).getPhysicalDisk(RBD_IMAGE_NAME);

Assert.assertFalse(storageProcessorSpy.isRbdImageGone(kvmStoragePoolMock, RBD_IMAGE_NAME));
}

/**
* librados reports a missing pool as -ENOENT too. That says nothing about the image, so it must not be read as
* the snapshot being gone.
*/
@Test
public void isRbdImageGoneTestPoolNotFound() {
Mockito.doThrow(new CloudRuntimeException("no pool", new RadosException("Failed to create the IoCTX", -2)))
.when(kvmStoragePoolMock).getPhysicalDisk(RBD_IMAGE_NAME);

Assert.assertFalse(storageProcessorSpy.isRbdImageGone(kvmStoragePoolMock, RBD_IMAGE_NAME));
}

/**
* Deleting a volume removes its RBD snapshots with the image. Deleting one of those snapshots afterwards must
* succeed at once, not wait for the image to appear and then fail.
*/
@Test
public void deleteSnapshotTestRbdImageGoneSucceedsWithoutWaitingForTheImage() {
KVMStorageProcessor processorSpy = prepareRbdSnapshotDelete(true);

Answer answer = processorSpy.deleteSnapshot(new DeleteCommand(snapshotObjectToMock));

Assert.assertTrue(answer.getResult());
Mockito.verify(storagePoolManager, Mockito.never()).getPhysicalDisk(Mockito.any(), Mockito.anyString(), Mockito.anyString());
}

@Test
public void deleteSnapshotTestRbdImagePresentTakesTheNormalPath() {
KVMStorageProcessor processorSpy = prepareRbdSnapshotDelete(false);
Mockito.doThrow(new CloudRuntimeException("lookup failed")).when(storagePoolManager)
.getPhysicalDisk(StoragePoolType.RBD, "pool-uuid", "volume-current-image");

Answer answer = processorSpy.deleteSnapshot(new DeleteCommand(snapshotObjectToMock));

Assert.assertFalse(answer.getResult());
Mockito.verify(storagePoolManager).getPhysicalDisk(StoragePoolType.RBD, "pool-uuid", "volume-current-image");
}

/**
* The volume's current image differs from the one the snapshot was taken of: the check looks at the snapshot's
* own image, and the removal path that follows keeps looking the image up by the volume's path as before.
*/
private KVMStorageProcessor prepareRbdSnapshotDelete(boolean imageGone) {
KVMStorageProcessor processorSpy = Mockito.spy(new KVMStorageProcessor(storagePoolManager, resource));
PrimaryDataStoreTO primaryStoreMock = Mockito.mock(PrimaryDataStoreTO.class);
Mockito.doReturn(StoragePoolType.RBD).when(primaryStoreMock).getPoolType();
Mockito.doReturn("pool-uuid").when(primaryStoreMock).getUuid();
Mockito.doReturn(primaryStoreMock).when(snapshotObjectToMock).getDataStore();
Mockito.doReturn(RBD_POOL_NAME + "/" + RBD_IMAGE_NAME + "/" + SNAPSHOT_NAME).when(snapshotObjectToMock).getPath();
Mockito.doReturn(volumeObjectToMock).when(snapshotObjectToMock).getVolume();
Mockito.lenient().doReturn("volume-current-image").when(volumeObjectToMock).getPath();
Mockito.doReturn(kvmStoragePoolMock).when(storagePoolManager).getStoragePool(StoragePoolType.RBD, "pool-uuid");
Mockito.doReturn(StoragePoolType.RBD).when(kvmStoragePoolMock).getType();
Mockito.doReturn(imageGone).when(processorSpy).isRbdImageGone(kvmStoragePoolMock, RBD_POOL_NAME + "/" + RBD_IMAGE_NAME);
return processorSpy;
}

/**
* Wires a mocked Ceph stack for {@link KVMStorageProcessor#takeRbdVolumeSnapshotOfStoppedVm} and returns the
* mocked disk. The Rbd instance is created inside the method under test, so it is mocked by construction.
Expand Down
Loading