Skip to content

Commit 1bc57d4

Browse files
Reuse the shared ONTAP snapshot delete during VM snapshot rollback so cleanup follows the normal delete path. (#127)
### Description This PR is to address a comment received on apache#13897 . `rollbackFlexVolSnapshot` now uses `deleteFlexVolSnapshotForCloudStackVolume` instead of issuing its own snapshot delete. <!-- When "Fixes: #<id>" is specified, the issue/PR will automatically be closed when this PR gets merged --> <!-- For addressing multiple issues/PRs, use multiple "Fixes: #<id>" --> <!-- Fixes: # --> <!--- ******************************************************************************* --> <!--- NOTE: AUTOMATION USES THE DESCRIPTIONS TO SET LABELS AND PRODUCE DOCUMENTATION. --> <!--- PLEASE PUT AN 'X' in only **ONE** box --> <!--- ******************************************************************************* --> ### Types of changes - [ ] Breaking change (fix or feature that would cause existing functionality to change) - [X] New feature (non-breaking change which adds functionality) - [ ] Bug fix (non-breaking change which fixes an issue) - [ ] Enhancement (improves an existing feature and functionality) - [ ] Cleanup (Code refactoring and cleanup, that may add test cases) - [ ] Build/CI - [ ] Test (unit or integration test code) ### Feature/Enhancement Scale or Bug Severity #### Feature/Enhancement Scale - [ ] Major - [ ] Minor #### Bug Severity - [ ] BLOCKER - [ ] Critical - [ ] Major - [X] Minor - [ ] Trivial ### Screenshots (if appropriate): ### How Has This Been Tested? <!-- Please describe in detail how you tested your changes. --> <!-- Include details of your testing environment, and the tests you ran to --> #### How did you try to break this feature and the system with this change? <!-- see how your change affects other areas of the code, etc. --> <!-- Please read the [CONTRIBUTING](https://github.com/apache/cloudstack/blob/main/CONTRIBUTING.md) document -->
1 parent ca43eed commit 1bc57d4

2 files changed

Lines changed: 75 additions & 6 deletions

File tree

‎plugins/storage/volume/ontap/src/main/java/org/apache/cloudstack/storage/vmsnapshot/OntapVMSnapshotStrategy.java‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,16 +1053,12 @@ void rollbackFlexVolSnapshot(FlexVolSnapshotDetail detail) {
10531053
try {
10541054
Map<String, String> poolDetails = storagePoolDetailsDao.listDetailsKeyPairs(detail.poolId);
10551055
StorageStrategy storageStrategy = resolveStorageStrategy(poolDetails);
1056-
SnapshotFeignClient client = storageStrategy.getSnapshotFeignClient();
1057-
String authHeader = storageStrategy.getAuthHeader();
10581056

10591057
logger.info("rollbackFlexVolSnapshot: Rolling back FlexVol snapshot [{}] (uuid={}) on FlexVol [{}]",
10601058
detail.snapshotName, detail.snapshotUuid, detail.flexVolUuid);
10611059

1062-
JobResponse jobResponse = client.deleteSnapshot(authHeader, detail.flexVolUuid, detail.snapshotUuid);
1063-
if (jobResponse != null && jobResponse.getJob() != null) {
1064-
storageStrategy.jobPollForSuccess(jobResponse.getJob().getUuid(), 10, 2000);
1065-
}
1060+
storageStrategy.deleteFlexVolSnapshotForCloudStackVolume(
1061+
detail.flexVolUuid, detail.snapshotUuid, detail.snapshotName);
10661062
} catch (Exception e) {
10671063
logger.error("rollbackFlexVolSnapshot: Rollback of FlexVol snapshot failed: {}", e.getMessage(), e);
10681064
}

‎plugins/storage/volume/ontap/src/test/java/org/apache/cloudstack/storage/vmsnapshot/OntapVMSnapshotStrategyTest.java‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@
1818
*/
1919
package org.apache.cloudstack.storage.vmsnapshot;
2020

21+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
2122
import static org.junit.jupiter.api.Assertions.assertEquals;
2223
import static org.junit.jupiter.api.Assertions.assertThrows;
2324
import static org.mockito.ArgumentMatchers.any;
2425
import static org.mockito.ArgumentMatchers.anyInt;
2526
import static org.mockito.ArgumentMatchers.anyLong;
27+
import static org.mockito.ArgumentMatchers.argThat;
2628
import static org.mockito.ArgumentMatchers.eq;
2729
import static org.mockito.Mockito.atLeastOnce;
2830
import static org.mockito.Mockito.doReturn;
@@ -58,6 +60,9 @@
5860
import org.apache.cloudstack.storage.feign.model.response.OntapResponse;
5961
import org.apache.cloudstack.storage.service.StorageStrategy;
6062
import org.apache.cloudstack.storage.to.VolumeObjectTO;
63+
import org.apache.logging.log4j.LogManager;
64+
import org.apache.logging.log4j.core.Appender;
65+
import org.apache.logging.log4j.core.Logger;
6166
import org.junit.jupiter.api.Test;
6267
import org.junit.jupiter.api.extension.ExtendWith;
6368
import org.mockito.InjectMocks;
@@ -686,6 +691,44 @@ void testFlexVolSnapshotDetail_Parse5Parts_ThrowsException() {
686691
() -> OntapVMSnapshotStrategy.FlexVolSnapshotDetail.parse("a::b::c::d::e"));
687692
}
688693

694+
@Test
695+
void testRollbackFlexVolSnapshot_UsesSharedDelete() {
696+
OntapVMSnapshotStrategy.FlexVolSnapshotDetail detail = new OntapVMSnapshotStrategy.FlexVolSnapshotDetail(
697+
"flexvol-uuid-1", "snapshot-uuid-1", "snapshot-name-1", "volume-301.qcow2", POOL_ID_1, "NFS3");
698+
doReturn(storageStrategy).when(strategy).resolveStorageStrategy(any());
699+
700+
assertDoesNotThrow(() -> strategy.rollbackFlexVolSnapshot(detail));
701+
702+
verify(storageStrategy).deleteFlexVolSnapshotForCloudStackVolume(
703+
"flexvol-uuid-1", "snapshot-uuid-1", "snapshot-name-1");
704+
verify(storageStrategy, never()).getSnapshotFeignClient();
705+
verify(storageStrategy, never()).getAuthHeader();
706+
verify(storageStrategy, never()).jobPollForSuccess(any(), anyInt(), anyInt());
707+
}
708+
709+
@Test
710+
void testRollbackFlexVolSnapshot_DeleteFailureIsLoggedAndNotThrown() {
711+
OntapVMSnapshotStrategy.FlexVolSnapshotDetail detail = new OntapVMSnapshotStrategy.FlexVolSnapshotDetail(
712+
"flexvol-uuid-1", "snapshot-uuid-1", "snapshot-name-1", "volume-301.qcow2", POOL_ID_1, "NFS3");
713+
doReturn(storageStrategy).when(strategy).resolveStorageStrategy(any());
714+
doThrow(new CloudRuntimeException("delete failed")).when(storageStrategy)
715+
.deleteFlexVolSnapshotForCloudStackVolume(any(), any(), any());
716+
717+
Logger logger = (Logger) LogManager.getLogger(OntapVMSnapshotStrategy.class);
718+
Appender appender = mock(Appender.class);
719+
when(appender.getName()).thenReturn("rollback-test-appender");
720+
when(appender.isStarted()).thenReturn(true);
721+
logger.addAppender(appender);
722+
try {
723+
assertDoesNotThrow(() -> strategy.rollbackFlexVolSnapshot(detail));
724+
725+
verify(appender).append(argThat(event -> event.getMessage().getFormattedMessage()
726+
.contains("Rollback of FlexVol snapshot failed: delete failed")));
727+
} finally {
728+
logger.removeAppender(appender);
729+
}
730+
}
731+
689732
// ══════════════════════════════════════════════════════════════════════════
690733
// Tests: buildSnapshotName
691734
// ══════════════════════════════════════════════════════════════════════════
@@ -861,6 +904,36 @@ void testTakeVMSnapshot_SingleFlexVolSuccess_UsesDirectSnapshotNotCg() throws Ex
861904
verify(vmSnapshotDetailsDao, atLeastOnce()).persist(any(VMSnapshotDetailsVO.class));
862905
}
863906

907+
@Test
908+
void testTakeVMSnapshot_RollbackDeleteFailurePreservesOriginalError() throws Exception {
909+
VMSnapshotVO vmSnapshot = createTakeSnapshotVmSnapshot();
910+
setupTakeSnapshotCommon(vmSnapshot);
911+
setupSingleVolumeForTakeSnapshot();
912+
String snapshotName = strategy.buildSnapshotName(vmSnapshot);
913+
setupSingleFlexVolFlowMocks(snapshotName);
914+
915+
FreezeThawVMAnswer freezeAnswer = mock(FreezeThawVMAnswer.class);
916+
when(freezeAnswer.getResult()).thenReturn(true);
917+
FreezeThawVMAnswer thawAnswer = mock(FreezeThawVMAnswer.class);
918+
when(thawAnswer.getResult()).thenReturn(true);
919+
when(agentMgr.send(eq(HOST_ID), any(FreezeThawVMCommand.class)))
920+
.thenReturn(freezeAnswer)
921+
.thenReturn(thawAnswer);
922+
doThrow(new CloudRuntimeException("snapshot persistence failed")).when(vmSnapshotDetailsDao)
923+
.persist(any(VMSnapshotDetailsVO.class));
924+
doThrow(new CloudRuntimeException("rollback delete failed")).when(storageStrategy)
925+
.deleteFlexVolSnapshotForCloudStackVolume(any(), any(), any());
926+
when(vmSnapshotDetailsDao.listDetails(SNAPSHOT_ID)).thenReturn(Collections.emptyList());
927+
doReturn(true).when(vmSnapshotHelper).vmSnapshotStateTransitTo(any(), eq(VMSnapshot.Event.OperationFailed));
928+
929+
CloudRuntimeException exception = assertThrows(CloudRuntimeException.class,
930+
() -> strategy.takeVMSnapshot(vmSnapshot));
931+
932+
assertEquals("snapshot persistence failed", exception.getMessage());
933+
verify(storageStrategy).deleteFlexVolSnapshotForCloudStackVolume(
934+
eq("flexvol-uuid-1"), any(), eq(snapshotName));
935+
}
936+
864937
@Test
865938
void testTakeVMSnapshot_TemporaryCgTwoPhaseSuccess_PersistsDetailsAndCleansUpCg() throws Exception {
866939
VMSnapshotVO vmSnapshot = createTakeSnapshotVmSnapshot();

0 commit comments

Comments
 (0)