SaketaChalamchala commented on code in PR #10774:
URL: https://github.com/apache/ozone/pull/10774#discussion_r3618491533
##########
hadoop-ozone/ozone-manager/src/test/java/org/apache/hadoop/ozone/om/snapshot/defrag/TestSnapshotDefragService.java:
##########
@@ -964,6 +965,67 @@ public void testCheckAndDefragSnapshotFailure(boolean
previousSnapshotExists) th
}
}
+ @ParameterizedTest
+ @ValueSource(booleans = {true, false})
+ public void testCheckpointCleanupOnDefragFailure(boolean
previousSnapshotExists) throws IOException {
+ SnapshotInfo snapshotInfo = createMockSnapshotInfo(UUID.randomUUID(),
"vol1", "bucket1", "snap2");
+ SnapshotInfo previousSnapshotInfo;
+ if (previousSnapshotExists) {
+ previousSnapshotInfo = createMockSnapshotInfo(UUID.randomUUID(), "vol1",
"bucket1", "snap1");
+
snapshotInfo.setPathPreviousSnapshotId(previousSnapshotInfo.getSnapshotId());
+ } else {
+ previousSnapshotInfo = null;
+ }
+
+ SnapshotChainManager chainManager = mock(SnapshotChainManager.class);
+ try (MockedStatic<SnapshotUtils> mockedStatic =
Mockito.mockStatic(SnapshotUtils.class)) {
+ mockedStatic.when(() -> SnapshotUtils.getSnapshotInfo(eq(ozoneManager),
eq(chainManager),
+ eq(snapshotInfo.getSnapshotId()))).thenReturn(snapshotInfo);
+ if (previousSnapshotExists) {
+ mockedStatic.when(() ->
SnapshotUtils.getSnapshotInfo(eq(ozoneManager), eq(chainManager),
+
eq(previousSnapshotInfo.getSnapshotId()))).thenReturn(previousSnapshotInfo);
+ }
+
+ SnapshotDefragService spyDefragService = Mockito.spy(defragService);
+ doReturn(Pair.of(true,
10)).when(spyDefragService).needsDefragmentation(eq(snapshotInfo));
+
+ @SuppressWarnings("resource") // Mock object, no actual resource
management needed
+ OmMetadataManagerImpl checkpointMetadataManager =
mock(OmMetadataManagerImpl.class);
+ File checkpointPath = tempDir.resolve("checkpoint_" +
System.nanoTime()).toAbsolutePath().toFile();
+ // Create actual checkpoint directory to verify cleanup
+ assertTrue(checkpointPath.mkdirs(), "Failed to create checkpoint
directory for test");
+ assertTrue(checkpointPath.exists(), "Checkpoint directory should exist
before defragmentation");
+
+ DBStore checkpointDBStore = mock(DBStore.class);
+ when(checkpointMetadataManager.getStore()).thenReturn(checkpointDBStore);
+ when(checkpointDBStore.getDbLocation()).thenReturn(checkpointPath);
+ doNothing().when(checkpointMetadataManager).close();
+
doReturn(checkpointMetadataManager).when(spyDefragService).createCheckpoint(any(),
any());
+
+ TablePrefixInfo prefixInfo = new TablePrefixInfo(Collections.emptyMap());
+ when(metadataManager.getTableBucketPrefix(anyString(),
anyString())).thenReturn(prefixInfo);
+
+ // Make the defrag operation throw IOException to simulate failure
+ IOException defragException = new IOException("Defrag failed");
+ if (previousSnapshotExists) {
+
Mockito.doThrow(defragException).when(spyDefragService).performIncrementalDefragmentation(
+ any(), any(), anyInt(), any(), any(), any());
+ } else {
+
Mockito.doThrow(defragException).when(spyDefragService).performFullDefragmentation(
+ any(), any(), any());
+ }
+
+ // Attempt defragmentation and verify exception is thrown
+ IOException thrownException =
org.junit.jupiter.api.Assertions.assertThrows(IOException.class,
+ () -> spyDefragService.checkAndDefragSnapshot(chainManager,
snapshotInfo.getSnapshotId()));
+ assertEquals("Defrag failed", thrownException.getMessage());
+
+ // Verify that checkpointMetadataManager.close() was called in the
finally block
+ // This confirms the finally block executed despite the exception
+ verify(checkpointMetadataManager).close();
Review Comment:
Also assert if temporary checkpoint has been cleaned up on failure to verify
the correctness of the patch.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]