From e17ca0e7434067920629bd2eb7659465c549fe36 Mon Sep 17 00:00:00 2001 From: Brad House Date: Tue, 1 Dec 2026 12:23:20 +0000 Subject: [PATCH] kvm: treat an RBD snapshot whose image is gone as deleted Deleting a volume on RBD removes its snapshots with the image. When snapshots are kept only on primary storage, the management server then deletes each of the volume's snapshots, and the agent looked up the image through KVMStoragePoolManager.getPhysicalDisk, which retries a missing volume 100 times, 3 seconds apart. After about 5 minutes the delete failed, so the volume delete took 5 minutes and its snapshots stayed listed as BackedUp: they could not be used, and every later deleteSnapshot failed the same way. Look the snapshot's image up once. If librbd reports it as not found, the snapshot is gone with it and the delete succeeds. Fixes #14931 Signed-off-by: Brad House --- .../kvm/storage/KVMStorageProcessor.java | 25 ++++++ .../kvm/storage/KVMStorageProcessorTest.java | 85 +++++++++++++++++++ 2 files changed, 110 insertions(+) diff --git a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/storage/KVMStorageProcessor.java b/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/storage/KVMStorageProcessor.java index 1fba9f3e96f0..efe392965f8d 100644 --- a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/storage/KVMStorageProcessor.java +++ b/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/storage/KVMStorageProcessor.java @@ -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; @@ -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 //; 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); @@ -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. * */ diff --git a/plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/storage/KVMStorageProcessorTest.java b/plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/storage/KVMStorageProcessorTest.java index 11d508d16468..d09352c784fa 100644 --- a/plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/storage/KVMStorageProcessorTest.java +++ b/plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/storage/KVMStorageProcessorTest.java @@ -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; @@ -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.