Copilot commented on code in PR #10951: URL: https://github.com/apache/ozone/pull/10951#discussion_r3716076924
########## hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/ozone/om/snapshot/TestOmSnapshotCheckpointDbContent.java: ########## @@ -0,0 +1,417 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.ozone.om.snapshot; + +import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_SNAPSHOT_DELETING_SERVICE_INTERVAL; +import static org.apache.hadoop.ozone.OzoneConsts.OM_KEY_PREFIX; +import static org.apache.hadoop.ozone.om.OMConfigKeys.OZONE_FILESYSTEM_SNAPSHOT_ENABLED_KEY; +import static org.apache.hadoop.ozone.om.OMConfigKeys.OZONE_SNAPSHOT_DEFRAG_SERVICE_INTERVAL; +import static org.apache.hadoop.ozone.om.OMConfigKeys.SNAPSHOT_DEFRAG_LIMIT_PER_TASK; +import static org.apache.hadoop.ozone.om.codec.OMDBDefinition.BUCKET_TABLE; +import static org.apache.hadoop.ozone.om.codec.OMDBDefinition.KEY_TABLE; +import static org.apache.hadoop.ozone.om.codec.OMDBDefinition.MULTIPART_INFO_TABLE; +import static org.apache.hadoop.ozone.om.codec.OMDBDefinition.OPEN_KEY_TABLE; +import static org.apache.hadoop.ozone.om.codec.OMDBDefinition.VOLUME_TABLE; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assumptions.assumeTrue; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.SortedMap; +import java.util.TreeMap; +import java.util.UUID; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +import org.apache.hadoop.hdds.conf.OzoneConfiguration; +import org.apache.hadoop.hdds.utils.IOUtils; +import org.apache.hadoop.hdds.utils.db.ManagedRawSSTFileReader; +import org.apache.hadoop.hdds.utils.db.RocksDBCheckpoint; +import org.apache.hadoop.hdds.utils.db.Table; +import org.apache.hadoop.hdds.utils.db.Table.KeyValue; +import org.apache.hadoop.hdds.utils.db.Table.KeyValueIterator; +import org.apache.hadoop.hdds.utils.db.TablePrefixInfo; +import org.apache.hadoop.ozone.MiniOzoneCluster; +import org.apache.hadoop.ozone.DataTestUtil; +import org.apache.hadoop.ozone.client.ObjectStore; +import org.apache.hadoop.ozone.client.OzoneBucket; +import org.apache.hadoop.ozone.client.OzoneClient; +import org.apache.hadoop.ozone.om.OMMetadataManager; +import org.apache.hadoop.ozone.om.OmMetadataManagerImpl; +import org.apache.hadoop.ozone.om.OmSnapshot; +import org.apache.hadoop.ozone.om.OmSnapshotManager; +import org.apache.hadoop.ozone.om.OzoneManager; +import org.apache.hadoop.ozone.om.helpers.BucketLayout; +import org.apache.hadoop.ozone.om.helpers.SnapshotInfo; +import org.apache.ozone.test.GenericTestUtils; +import org.apache.ratis.util.function.UncheckedAutoCloseableSupplier; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Timeout; + +/** + * HDDS-13217: verify snapshot checkpoint DB content is preserved across defrag iterations. + * + * <p>After defrag compacts a snapshot checkpoint (S' = compact(S)), the on-disk metadata for that + * bucket prefix in the defragged checkpoint must still match the original checkpoint taken at + * snapshot creation time (version 0). + * + * <p>Version-0 checkpoint directories are removed after defrag, so baselines are captured before + * the first defrag iteration and compared against the active snapshot after defrag completes. + * + * <p>Each test method starts a fresh MiniOzone cluster to avoid cross-test interference on the + * global snapshot defrag chain. + */ +@Timeout(value = 15, unit = TimeUnit.MINUTES) +public class TestOmSnapshotCheckpointDbContent { + + private static final byte[] TEST_KEY_CONTENT = new byte[] {0x61, 0x62, 0x63}; + private static final int CHECKPOINT_WAIT_MS = 120_000; + private static final int DEFRAG_WAIT_MS = 600_000; + + private MiniOzoneCluster cluster; + private OzoneConfiguration conf; + private OzoneClient client; + private ObjectStore store; + + @BeforeEach + void initCluster() throws Exception { + assumeTrue(ManagedRawSSTFileReader.tryLoadLibrary(), + "Snapshot defrag requires rocks-tools native library"); + + conf = new OzoneConfiguration(); + conf.setBoolean(OZONE_FILESYSTEM_SNAPSHOT_ENABLED_KEY, true); + conf.setInt(OZONE_SNAPSHOT_DEFRAG_SERVICE_INTERVAL, 7200); + conf.setInt(SNAPSHOT_DEFRAG_LIMIT_PER_TASK, 10); + conf.setTimeDuration(OZONE_SNAPSHOT_DELETING_SERVICE_INTERVAL, 1, TimeUnit.SECONDS); Review Comment: OZONE_SNAPSHOT_DEFRAG_SERVICE_INTERVAL is read via getTimeDuration(..., TimeUnit.MILLISECONDS) (KeyManagerImpl), so setting it with setInt(7200) configures a 7.2s interval (milliseconds) rather than 7200s. That can allow background defrag to run during snapshot creation/baseline capture and make this test flaky. Set it explicitly as a time duration with units to keep the periodic service effectively idle while still initialized for manual triggers. ########## hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/ozone/om/snapshot/TestOmSnapshotCheckpointDbContent.java: ########## @@ -0,0 +1,417 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.ozone.om.snapshot; + +import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_SNAPSHOT_DELETING_SERVICE_INTERVAL; +import static org.apache.hadoop.ozone.OzoneConsts.OM_KEY_PREFIX; +import static org.apache.hadoop.ozone.om.OMConfigKeys.OZONE_FILESYSTEM_SNAPSHOT_ENABLED_KEY; +import static org.apache.hadoop.ozone.om.OMConfigKeys.OZONE_SNAPSHOT_DEFRAG_SERVICE_INTERVAL; +import static org.apache.hadoop.ozone.om.OMConfigKeys.SNAPSHOT_DEFRAG_LIMIT_PER_TASK; +import static org.apache.hadoop.ozone.om.codec.OMDBDefinition.BUCKET_TABLE; +import static org.apache.hadoop.ozone.om.codec.OMDBDefinition.KEY_TABLE; +import static org.apache.hadoop.ozone.om.codec.OMDBDefinition.MULTIPART_INFO_TABLE; +import static org.apache.hadoop.ozone.om.codec.OMDBDefinition.OPEN_KEY_TABLE; +import static org.apache.hadoop.ozone.om.codec.OMDBDefinition.VOLUME_TABLE; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assumptions.assumeTrue; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.SortedMap; +import java.util.TreeMap; +import java.util.UUID; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +import org.apache.hadoop.hdds.conf.OzoneConfiguration; +import org.apache.hadoop.hdds.utils.IOUtils; +import org.apache.hadoop.hdds.utils.db.ManagedRawSSTFileReader; +import org.apache.hadoop.hdds.utils.db.RocksDBCheckpoint; +import org.apache.hadoop.hdds.utils.db.Table; +import org.apache.hadoop.hdds.utils.db.Table.KeyValue; +import org.apache.hadoop.hdds.utils.db.Table.KeyValueIterator; +import org.apache.hadoop.hdds.utils.db.TablePrefixInfo; +import org.apache.hadoop.ozone.MiniOzoneCluster; +import org.apache.hadoop.ozone.DataTestUtil; +import org.apache.hadoop.ozone.client.ObjectStore; +import org.apache.hadoop.ozone.client.OzoneBucket; +import org.apache.hadoop.ozone.client.OzoneClient; +import org.apache.hadoop.ozone.om.OMMetadataManager; +import org.apache.hadoop.ozone.om.OmMetadataManagerImpl; +import org.apache.hadoop.ozone.om.OmSnapshot; +import org.apache.hadoop.ozone.om.OmSnapshotManager; +import org.apache.hadoop.ozone.om.OzoneManager; +import org.apache.hadoop.ozone.om.helpers.BucketLayout; +import org.apache.hadoop.ozone.om.helpers.SnapshotInfo; +import org.apache.ozone.test.GenericTestUtils; +import org.apache.ratis.util.function.UncheckedAutoCloseableSupplier; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Timeout; + +/** + * HDDS-13217: verify snapshot checkpoint DB content is preserved across defrag iterations. + * + * <p>After defrag compacts a snapshot checkpoint (S' = compact(S)), the on-disk metadata for that + * bucket prefix in the defragged checkpoint must still match the original checkpoint taken at + * snapshot creation time (version 0). + * + * <p>Version-0 checkpoint directories are removed after defrag, so baselines are captured before + * the first defrag iteration and compared against the active snapshot after defrag completes. + * + * <p>Each test method starts a fresh MiniOzone cluster to avoid cross-test interference on the + * global snapshot defrag chain. + */ +@Timeout(value = 15, unit = TimeUnit.MINUTES) +public class TestOmSnapshotCheckpointDbContent { + + private static final byte[] TEST_KEY_CONTENT = new byte[] {0x61, 0x62, 0x63}; + private static final int CHECKPOINT_WAIT_MS = 120_000; + private static final int DEFRAG_WAIT_MS = 600_000; + + private MiniOzoneCluster cluster; + private OzoneConfiguration conf; + private OzoneClient client; + private ObjectStore store; + + @BeforeEach + void initCluster() throws Exception { + assumeTrue(ManagedRawSSTFileReader.tryLoadLibrary(), + "Snapshot defrag requires rocks-tools native library"); + + conf = new OzoneConfiguration(); + conf.setBoolean(OZONE_FILESYSTEM_SNAPSHOT_ENABLED_KEY, true); + conf.setInt(OZONE_SNAPSHOT_DEFRAG_SERVICE_INTERVAL, 7200); + conf.setInt(SNAPSHOT_DEFRAG_LIMIT_PER_TASK, 10); + conf.setTimeDuration(OZONE_SNAPSHOT_DELETING_SERVICE_INTERVAL, 1, TimeUnit.SECONDS); + + cluster = MiniOzoneCluster.newBuilder(conf).setNumDatanodes(3).build(); + cluster.waitForClusterToBeReady(); + client = cluster.newClient(); + store = client.getObjectStore(); + } + + @AfterEach + void shutdownCluster() { + IOUtils.closeQuietly(client); + IOUtils.closeQuietly(cluster); + } + + /** + * Test#1 from HDDS-13217: create S1, S2, S3 on one bucket, run defrag, and verify each + * defragged checkpoint still matches its version-0 baseline. + */ + @Test + public void testDefragPreservesSnapshotCheckpointContent() + throws Exception { + ThreeSnapshotSetup setup = createThreeSnapshotsOnNewBucket(); + triggerDefragUntilDone(setup.snapshots); + assertCheckpointMatchesBaseline(setup.baselines, setup.snapshots); + } + + /** + * Test#2 from HDDS-13217: after an initial defrag pass, delete the middle snapshot, run defrag + * again, and verify the remaining youngest snapshot checkpoint still matches its baseline. + */ + @Test + public void testDefragAfterSnapshotDeletePreservesRemainingSnapshot() + throws Exception { + ThreeSnapshotSetup setup = createThreeSnapshotsOnNewBucket(); + triggerDefragUntilDone(setup.snapshots); + + SnapshotInfo s2 = setup.snapshots.get(1); + SnapshotInfo s3 = setup.snapshots.get(2); + int s3VersionAfterFirstDefrag = readSnapshotVersion(s3); + + store.deleteSnapshot(setup.volumeName, setup.bucketName, s2.getName()); + waitForSnapshotPurged(s2); + // Reload S3 so pathPreviousSnapshotId reflects the purged chain, not deleted S2. + s3 = loadSnapshotInfo(setup.volumeName, setup.bucketName, s3.getName()); + + triggerDefragUntilVersionIncreases(s3, s3VersionAfterFirstDefrag); + + assertCheckpointMatchesBaseline( + Collections.singletonMap(s3.getSnapshotId(), + setup.baselines.get(s3.getSnapshotId())), + Arrays.asList(s3)); + } + + private ThreeSnapshotSetup createThreeSnapshotsOnNewBucket() + throws IOException, InterruptedException, TimeoutException { + OzoneBucket bucket = + DataTestUtil.createVolumeAndBucket(client, BucketLayout.OBJECT_STORE); + String volumeName = bucket.getVolumeName(); + String bucketName = bucket.getName(); + + DataTestUtil.createKey(bucket, "key-s1", TEST_KEY_CONTENT); + store.createSnapshot(volumeName, bucketName, "snap-s1"); + + DataTestUtil.createKey(bucket, "key-s2", TEST_KEY_CONTENT); + store.createSnapshot(volumeName, bucketName, "snap-s2"); + + DataTestUtil.createKey(bucket, "key-s3", TEST_KEY_CONTENT); + store.createSnapshot(volumeName, bucketName, "snap-s3"); + + List<SnapshotInfo> snapshots = Arrays.asList( + loadSnapshotInfo(volumeName, bucketName, "snap-s1"), + loadSnapshotInfo(volumeName, bucketName, "snap-s2"), + loadSnapshotInfo(volumeName, bucketName, "snap-s3")); + + for (SnapshotInfo snapshotInfo : snapshots) { + waitForCheckpointReady(snapshotInfo); + } + + OMMetadataManager liveMm = cluster.getOzoneManager().getMetadataManager(); + TablePrefixInfo prefixes = liveMm.getTableBucketPrefix(volumeName, bucketName); + Map<UUID, SnapshotBaseline> baselines = new HashMap<>(); + for (SnapshotInfo snapshotInfo : snapshots) { + baselines.put(snapshotInfo.getSnapshotId(), captureBaseline(snapshotInfo, prefixes)); + } + return new ThreeSnapshotSetup(volumeName, bucketName, snapshots, baselines); + } + + private SnapshotInfo loadSnapshotInfo(String volumeName, String bucketName, + String snapshotName) throws IOException { + OzoneManager om = cluster.getOzoneManager(); + SnapshotInfo snapshotInfo = om.getMetadataManager().getSnapshotInfoTable().get( + SnapshotInfo.getTableKey(volumeName, bucketName, snapshotName)); + assertNotNull(snapshotInfo, "Snapshot row should exist for " + snapshotName); + assertEquals(snapshotName, snapshotInfo.getName()); + return snapshotInfo; + } + + private void waitForCheckpointReady(SnapshotInfo snapshotInfo) + throws TimeoutException, InterruptedException { + String currentPath = OmSnapshotManager.getSnapshotPath(conf, snapshotInfo, 0) + + OM_KEY_PREFIX + "CURRENT"; + GenericTestUtils.waitFor(() -> new File(currentPath).exists(), 1000, CHECKPOINT_WAIT_MS); + } + + private void waitForSnapshotPurged(SnapshotInfo snapshotInfo) + throws TimeoutException, InterruptedException { + OzoneManager om = cluster.getOzoneManager(); + GenericTestUtils.waitFor(() -> { + try { + return om.getMetadataManager().getSnapshotInfoTable() + .get(snapshotInfo.getTableKey()) == null; + } catch (IOException e) { + return false; + } + }, 1000, CHECKPOINT_WAIT_MS); + } + + /** + * Wait for a follow-up defrag pass after snapshot-chain rewiring (e.g. middle snapshot purge). + * Unlike {@link #triggerDefragUntilDone}, this requires the snapshot version to increase so we + * do not treat an already-defragged checkpoint from an earlier pass as complete. + */ + private void triggerDefragUntilVersionIncreases(SnapshotInfo snapshotInfo, + int baselineVersion) throws TimeoutException, InterruptedException { + OzoneManager om = cluster.getOzoneManager(); + GenericTestUtils.waitFor(() -> { + try { + if (readSnapshotVersion(snapshotInfo) > baselineVersion + && isSnapshotDefragComplete(snapshotInfo)) { + return true; + } + om.triggerSnapshotDefrag(false); + return readSnapshotVersion(snapshotInfo) > baselineVersion + && isSnapshotDefragComplete(snapshotInfo); + } catch (IOException e) { + return false; + } + }, 2000, DEFRAG_WAIT_MS); + } + + private void triggerDefragUntilDone(List<SnapshotInfo> snapshots) + throws TimeoutException, InterruptedException { + OzoneManager om = cluster.getOzoneManager(); + for (SnapshotInfo snapshotInfo : snapshots) { + GenericTestUtils.waitFor(() -> { + if (isSnapshotDefragComplete(snapshotInfo)) { + return true; + } + try { + om.triggerSnapshotDefrag(false); + } catch (IOException e) { + return false; + } + return isSnapshotDefragComplete(snapshotInfo); + }, 2000, DEFRAG_WAIT_MS); + } + } Review Comment: triggerDefragUntilDone waits up to DEFRAG_WAIT_MS for each snapshot sequentially, so the total wait budget is DEFRAG_WAIT_MS * snapshots.size(). This can easily exceed the per-test @Timeout(15 MINUTES) and increases flakiness. Consider waiting for all snapshots to become defrag-complete within a single overall deadline instead of per-snapshot deadlines. -- 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]
