This is an automated email from the ASF dual-hosted git repository.

Pearl1594 pushed a commit to branch clvm-ng-vm-snapshots
in repository https://gitbox.apache.org/repos/asf/cloudstack.git

commit 0283f90462d52588e04360a0e964d7d970051690
Author: Pearl1594 <[email protected]>
AuthorDate: Thu Nov 26 12:05:24 2026 -0500

    CLVM_NG: retry activating VM snapshot layers while another host holds their 
lock
---
 .../hypervisor/kvm/storage/ClvmStorageAdaptor.java | 42 ++++++++++++++++++++--
 .../kvm/storage/ClvmStorageAdaptorTest.java        | 27 ++++++++++++++
 2 files changed, 66 insertions(+), 3 deletions(-)

diff --git 
a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/storage/ClvmStorageAdaptor.java
 
b/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/storage/ClvmStorageAdaptor.java
index 91741993f2d..f27697f7057 100644
--- 
a/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/storage/ClvmStorageAdaptor.java
+++ 
b/plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/storage/ClvmStorageAdaptor.java
@@ -27,7 +27,6 @@ import java.util.List;
 import java.util.Map;
 import java.util.stream.Collectors;
 
-import com.cloud.hypervisor.kvm.resource.LibvirtComputingResource;
 import com.cloud.hypervisor.kvm.resource.LibvirtConnection;
 import com.cloud.hypervisor.kvm.resource.LibvirtStoragePoolDef;
 import com.cloud.hypervisor.kvm.storage.clvm.ClvmBackingChain;
@@ -53,6 +52,9 @@ import org.libvirt.StorageVol;
  * using direct LVM commands instead of libvirt for volume operations.
  */
 public class ClvmStorageAdaptor extends LibvirtStorageAdaptor {
+    private static final long LOCK_RETRY_TIMEOUT_MILLIS = 180000L;
+    private static final long LOCK_RETRY_INTERVAL_MILLIS = 10000L;
+    private static final String LOCKED_BY_OTHER_HOST = "locked by other host";
     private static final String SIDECAR_LV_PREFIX = "vmsnapshot-";
     private static final String SIDECAR_SIZE_TAG_PREFIX = "sidecarsize_";
 
@@ -700,7 +702,7 @@ public class ClvmStorageAdaptor extends 
LibvirtStorageAdaptor {
 
                     if (!isShared || !isActive) {
                         logger.info("Template LV {} is not in shared mode 
(attr: {}). Activating in shared mode.", templatePath, lvAttr);
-                        
LibvirtComputingResource.setClvmVolumeToSharedMode(templatePath);
+                        activateSharedWithRetry(templatePath);
                     } else {
                         logger.debug("Template LV {} is already in shared mode 
(attr: {})", templatePath, lvAttr);
                     }
@@ -1031,7 +1033,41 @@ public class ClvmStorageAdaptor extends 
LibvirtStorageAdaptor {
         String attr = parser.getLines() == null ? "" : 
parser.getLines().trim();
         if (attr.indexOf('a') < 0) {
             logger.info("LV {} is inactive, activating it in shared mode so it 
can back a VM snapshot delta.", lvPath);
-            LibvirtComputingResource.setClvmVolumeToSharedMode(lvPath);
+            activateSharedWithRetry(lvPath);
+        }
+    }
+
+    /**
+     * Activates the LV in shared mode. While another host holds its lock, 
which also happens after a host failure until the lease of that host expires,
+     * the activation is retried for the same time the management server 
retries the lock of a volume.
+     */
+    protected void activateSharedWithRetry(String lvPath) {
+        long deadline = System.currentTimeMillis() + LOCK_RETRY_TIMEOUT_MILLIS;
+        while (true) {
+            Script activate = new Script("lvchange", Duration.millis(60000), 
logger);
+            activate.add("-asy", lvPath);
+            String result = activate.execute();
+            if (result == null) {
+                return;
+            }
+            if (!result.contains(LOCKED_BY_OTHER_HOST) || 
System.currentTimeMillis() >= deadline) {
+                logger.warn("Failed to activate LV {} in shared mode: {}", 
lvPath, result);
+                return;
+            }
+            logger.info("LV {} is locked by another host, retrying its 
activation in {} s.", lvPath, LOCK_RETRY_INTERVAL_MILLIS / 1000);
+            if (!sleepBeforeLockRetry()) {
+                return;
+            }
+        }
+    }
+
+    protected boolean sleepBeforeLockRetry() {
+        try {
+            Thread.sleep(LOCK_RETRY_INTERVAL_MILLIS);
+            return true;
+        } catch (InterruptedException e) {
+            Thread.currentThread().interrupt();
+            return false;
         }
     }
 
diff --git 
a/plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/storage/ClvmStorageAdaptorTest.java
 
b/plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/storage/ClvmStorageAdaptorTest.java
index 6310b02ab4d..e3da7a51625 100644
--- 
a/plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/storage/ClvmStorageAdaptorTest.java
+++ 
b/plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/storage/ClvmStorageAdaptorTest.java
@@ -254,6 +254,33 @@ public class ClvmStorageAdaptorTest {
         Mockito.verify(mockScriptConstruction.constructed().get(0)).add("-n", 
"vmsnapshot-memory-7", "-L", "402653184B", "--yes", "testvg");
     }
 
+    @Test
+    public void 
testActivateSharedWithRetryRetriesWhileAnotherHostHoldsTheLock() {
+        Mockito.doReturn(true).when(clvmStorageAdaptor).sleepBeforeLockRetry();
+        mockScriptConstruction = Mockito.mockConstruction(Script.class,
+            (mock, context) -> {
+                when(mock.execute()).thenReturn(context.getCount() <= 2 ? "  
LV locked by other host: vg/lv" : null);
+            });
+
+        clvmStorageAdaptor.activateSharedWithRetry("/dev/vg/lv");
+
+        assertEquals("Two refused attempts and the one that succeeded", 3, 
mockScriptConstruction.constructed().size());
+        Mockito.verify(clvmStorageAdaptor, 
Mockito.times(2)).sleepBeforeLockRetry();
+        
Mockito.doCallRealMethod().when(clvmStorageAdaptor).sleepBeforeLockRetry();
+    }
+
+    @Test
+    public void testActivateSharedWithRetryDoesNotRetryOtherFailures() {
+        mockScriptConstruction = Mockito.mockConstruction(Script.class,
+            (mock, context) -> {
+                when(mock.execute()).thenReturn("  Failed to find logical 
volume");
+            });
+
+        clvmStorageAdaptor.activateSharedWithRetry("/dev/vg/lv");
+
+        assertEquals(1, mockScriptConstruction.constructed().size());
+    }
+
     @Test
     public void testCreateClvmVolume_Success() throws Exception {
         String volumeName = UUID.randomUUID().toString();

Reply via email to