aswinshakil commented on code in PR #4571: URL: https://github.com/apache/ozone/pull/4571#discussion_r1196800151
########## hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/fs/ozone/TestSnapshotDeletingService.java: ########## @@ -0,0 +1,465 @@ +/* + * 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.fs.ozone; + +import org.apache.hadoop.hdds.client.ReplicationFactor; +import org.apache.hadoop.hdds.client.ReplicationType; +import org.apache.hadoop.hdds.conf.OzoneConfiguration; +import org.apache.hadoop.hdds.utils.IOUtils; +import org.apache.hadoop.hdds.utils.db.Table; +import org.apache.hadoop.ozone.MiniOzoneCluster; +import org.apache.hadoop.ozone.TestDataUtil; +import org.apache.hadoop.ozone.client.BucketArgs; +import org.apache.hadoop.ozone.client.OzoneBucket; +import org.apache.hadoop.ozone.client.OzoneClient; +import org.apache.hadoop.ozone.om.OMConfigKeys; +import org.apache.hadoop.ozone.om.OmMetadataManagerImpl; +import org.apache.hadoop.ozone.om.OmSnapshot; +import org.apache.hadoop.ozone.om.OzoneManager; +import org.apache.hadoop.ozone.om.helpers.BucketLayout; +import org.apache.hadoop.ozone.om.helpers.OmDirectoryInfo; +import org.apache.hadoop.ozone.om.helpers.OmKeyInfo; +import org.apache.hadoop.ozone.om.helpers.RepeatedOmKeyInfo; +import org.apache.hadoop.ozone.om.helpers.SnapshotInfo; +import org.apache.hadoop.ozone.om.service.DirectoryDeletingService; +import org.apache.hadoop.ozone.om.service.SnapshotDeletingService; +import org.apache.ozone.test.GenericTestUtils; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.util.Objects; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_ACL_ENABLED; +import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_BLOCK_DELETING_SERVICE_INTERVAL; +import static org.apache.hadoop.ozone.om.OmSnapshotManager.getSnapshotPrefix; +import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_SNAPSHOT_DELETING_SERVICE_INTERVAL; +import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_SNAPSHOT_DELETING_SERVICE_TIMEOUT; +import static org.junit.jupiter.api.Assertions.fail; + +/** + * Test Snapshot Deleting Service. + */ +public class TestSnapshotDeletingService { + + private static final Logger LOG = + LoggerFactory.getLogger(TestSnapshotDeletingService.class); + private static boolean omRatisEnabled = true; + private static final String CONTENT = "testContent"; + + private MiniOzoneCluster cluster; + private OzoneManager om; + private OzoneBucket bucket1; + private OzoneClient client; + private static final String VOLUME_NAME = "vol1"; + private static final String BUCKET_NAME_ONE = "bucket1"; + private static final String BUCKET_NAME_TWO = "bucket2"; + + @BeforeEach + public void setup() throws Exception { + OzoneConfiguration conf = new OzoneConfiguration(); + conf.setTimeDuration(OZONE_BLOCK_DELETING_SERVICE_INTERVAL, 100, + TimeUnit.MILLISECONDS); + conf.setTimeDuration(OZONE_SNAPSHOT_DELETING_SERVICE_INTERVAL, + 100, TimeUnit.MILLISECONDS); + conf.setTimeDuration(OZONE_SNAPSHOT_DELETING_SERVICE_TIMEOUT, + 10000, TimeUnit.MILLISECONDS); + conf.setInt(OMConfigKeys.OZONE_DIR_DELETING_SERVICE_INTERVAL, 2000); + conf.setInt(OMConfigKeys.OZONE_PATH_DELETING_LIMIT_PER_TASK, 5); + conf.setTimeDuration(OZONE_BLOCK_DELETING_SERVICE_INTERVAL, 100, + TimeUnit.MILLISECONDS); + conf.setBoolean(OMConfigKeys.OZONE_OM_RATIS_ENABLE_KEY, omRatisEnabled); + conf.setBoolean(OZONE_ACL_ENABLED, true); + cluster = MiniOzoneCluster.newBuilder(conf) + .setNumDatanodes(3) + .build(); + cluster.waitForClusterToBeReady(); + client = cluster.newClient(); + om = cluster.getOzoneManager(); + bucket1 = TestDataUtil.createVolumeAndBucket( + client, VOLUME_NAME, BUCKET_NAME_ONE, BucketLayout.DEFAULT); + } + + @AfterEach + public void teardown() { + IOUtils.closeQuietly(client); + if (cluster != null) { + cluster.shutdown(); + } + } + + @Test + public void testSnapshotSplitAndMove() throws Exception { + SnapshotDeletingService snapshotDeletingService = (SnapshotDeletingService) + om.getKeyManager().getSnapshotDeletingService(); + Table<String, SnapshotInfo> snapshotInfoTable = + om.getMetadataManager().getSnapshotInfoTable(); + + createSnapshotDataForBucket1(); + + assertTableRowCount(snapshotInfoTable, 2); + GenericTestUtils.waitFor(() -> snapshotDeletingService + .getSuccessfulRunCount() >= 1, 1000, 10000); + + OmSnapshot bucket1snap3 = (OmSnapshot) om.getOmSnapshotManager() + .checkForSnapshot(VOLUME_NAME, BUCKET_NAME_ONE, + getSnapshotPrefix("bucket1snap3")); + + // Check bucket1key1 added to next non deleted snapshot db. + RepeatedOmKeyInfo omKeyInfo = + bucket1snap3.getMetadataManager() + .getDeletedTable().get("/vol1/bucket1/bucket1key1"); + Assertions.assertNotNull(omKeyInfo); + } + + @Test + public void testMultipleSnapshotKeyReclaim() throws Exception { + + SnapshotDeletingService snapshotDeletingService = (SnapshotDeletingService) + om.getKeyManager().getSnapshotDeletingService(); + Table<String, RepeatedOmKeyInfo> deletedTable = + om.getMetadataManager().getDeletedTable(); + + createSnapshotDataForBucket1(); + + BucketArgs bucketArgs = new BucketArgs.Builder() + .setBucketLayout(BucketLayout.LEGACY) + .build(); + + OzoneBucket bucket2 = TestDataUtil.createBucket( + client, VOLUME_NAME, bucketArgs, BUCKET_NAME_TWO); + // Create key1 and key2 + TestDataUtil.createKey(bucket2, "bucket2key1", ReplicationFactor.THREE, + ReplicationType.RATIS, CONTENT); + TestDataUtil.createKey(bucket2, "bucket2key2", ReplicationFactor.THREE, + ReplicationType.RATIS, CONTENT); + + // Create Snapshot + client.getObjectStore().createSnapshot(VOLUME_NAME, BUCKET_NAME_TWO, + "bucket2snap1"); + + // Both key 1 and key 2 can be reclaimed when Snapshot 1 is deleted. + client.getProxy().deleteKey(VOLUME_NAME, BUCKET_NAME_TWO, + "bucket2key1", false); + client.getProxy().deleteKey(VOLUME_NAME, BUCKET_NAME_TWO, + "bucket2key2", false); + assertTableRowCount(om.getMetadataManager().getDeletedTable(), 2); + SnapshotInfo delSnapInfo = om.getMetadataManager().getSnapshotInfoTable() + .get("/vol1/bucket2/bucket2snap1"); + + client.getObjectStore().deleteSnapshot(VOLUME_NAME, BUCKET_NAME_TWO, + "bucket2snap1"); + + GenericTestUtils.waitFor(() -> snapshotDeletingService + .getSuccessfulRunCount() >= 1, 1000, 10000); + assertTableRowCount(deletedTable, 2); + // Check bucket2key1 added active db as it can be reclaimed. + RepeatedOmKeyInfo omKeyInfo1 = om.getMetadataManager() + .getDeletedTable().get("/vol1/bucket2/bucket2key1"); + + // Check bucket2key2 added active db as it can be reclaimed. + RepeatedOmKeyInfo omKeyInfo2 = om.getMetadataManager() + .getDeletedTable().get("/vol1/bucket2/bucket2key2"); + + Assertions.assertNotNull(omKeyInfo1); + Assertions.assertNotNull(omKeyInfo2); + + verifySnapshotChain(delSnapInfo, null); + } + + @Test + public void testSnapshotWithFSO() throws Exception { + Table<String, OmDirectoryInfo> dirTable = + om.getMetadataManager().getDirectoryTable(); + Table<String, OmKeyInfo> keyTable = + om.getMetadataManager().getFileTable(); + Table<String, RepeatedOmKeyInfo> deletedTable = + om.getMetadataManager().getDeletedTable(); + Table<String, OmKeyInfo> deletedDirTable = + om.getMetadataManager().getDeletedDirTable(); + Table<String, String> renamedKeyTable = + om.getMetadataManager().getSnapshotRenamedKeyTable(); + DirectoryDeletingService dirDeletingService = (DirectoryDeletingService) + om.getKeyManager().getDirDeletingService(); + // TODO: [SNAPSHOT] Enable after HDDS-8064. + dirDeletingService.suspend(); + + BucketArgs bucketArgs = new BucketArgs.Builder() + .setBucketLayout(BucketLayout.FILE_SYSTEM_OPTIMIZED) + .build(); + + OzoneBucket bucket2 = TestDataUtil.createBucket( + client, VOLUME_NAME, bucketArgs, BUCKET_NAME_TWO); + + // Create 10 keys + for (int i = 1; i <= 10; i++) { + TestDataUtil.createKey(bucket2, "key" + i, ReplicationFactor.THREE, + ReplicationType.RATIS, CONTENT); + } + + // Create Directory and Sub + for (int i = 1; i <= 3; i++) { + String parent = "parent" + i; + client.getProxy().createDirectory(VOLUME_NAME, + BUCKET_NAME_TWO, parent); + for (int j = 1; j <= 3; j++) { + String childFile = "/childFile" + j; + String childDir = "/childDir" + j; + client.getProxy().createDirectory(VOLUME_NAME, + BUCKET_NAME_TWO, parent + childDir); + TestDataUtil.createKey(bucket2, parent + childFile, + ReplicationFactor.THREE, ReplicationType.RATIS, CONTENT); + } + } + + // Total 12 dirs, 19 keys. + assertTableRowCount(dirTable, 12); + assertTableRowCount(keyTable, 19); + assertTableRowCount(deletedDirTable, 0); + + // Create Snapshot1 + client.getObjectStore().createSnapshot(VOLUME_NAME, BUCKET_NAME_TWO, + "snap1"); + + // Delete 5 Keys + for (int i = 1; i <= 5; i++) { + client.getProxy().deleteKey(VOLUME_NAME, BUCKET_NAME_TWO, + "key" + i, false); + } + // Rename Keys 3 keys + for (int i = 6; i <= 8; i++) { + client.getProxy().renameKey(VOLUME_NAME, BUCKET_NAME_TWO, "key" + i, + "renamedKey" + i); + } + // Delete 2 Dirs + for (int i = 1; i <= 2; i++) { + client.getProxy().deleteKey(VOLUME_NAME, BUCKET_NAME_TWO, "/parent" + i, + true); + } + + assertTableRowCount(renamedKeyTable, 3); + // Delete Renamed Keys + for (int i = 6; i <= 8; i++) { + client.getProxy().deleteKey(VOLUME_NAME, BUCKET_NAME_TWO, + "renamedKey" + i, false); + } + + assertTableRowCount(deletedTable, 8); + assertTableRowCount(deletedDirTable, 2); + assertTableRowCount(dirTable, 10); + assertTableRowCount(renamedKeyTable, 3); + + // Create Snapshot2 + client.getObjectStore().createSnapshot(VOLUME_NAME, BUCKET_NAME_TWO, + "snap2"); + + // Once snapshot is taken renamedTable, deletedTable should be cleaned + assertTableRowCount(renamedKeyTable, 0); + assertTableRowCount(deletedTable, 0); + // TODO: [SNAPSHOT] Once HDDS-8064 is done, This should be + // zero. As there shouldn't be directory expansion happening. + assertTableRowCount(deletedDirTable, 2); + + // Delete 2 more keys + for (int i = 9; i <= 10; i++) { + client.getProxy().deleteKey(VOLUME_NAME, BUCKET_NAME_TWO, + "key" + i, false); + } + + assertTableRowCount(deletedTable, 2); + + // Create Snapshot3 + client.getObjectStore().createSnapshot(VOLUME_NAME, BUCKET_NAME_TWO, + "snap3"); + + assertTableRowCount(renamedKeyTable, 0); + assertTableRowCount(deletedDirTable, 2); + assertTableRowCount(deletedTable, 0); + assertTableRowCount(keyTable, 9); + SnapshotInfo deletedSnap = om.getMetadataManager() + .getSnapshotInfoTable().get("/vol1/bucket2/snap2"); + + client.getObjectStore().deleteSnapshot(VOLUME_NAME, BUCKET_NAME_TWO, + "snap2"); + + // Once all the tables are moved, the snapshot is deleted + assertTableRowCount(om.getMetadataManager().getSnapshotInfoTable(), 2); + + verifySnapshotChain(deletedSnap, "/vol1/bucket2/snap3"); + OmSnapshot snap3 = (OmSnapshot) om.getOmSnapshotManager() + .checkForSnapshot(VOLUME_NAME, BUCKET_NAME_TWO, + getSnapshotPrefix("snap3")); + + Table<String, OmKeyInfo> snapDeletedDirTable = + snap3.getMetadataManager().getDeletedDirTable(); + Table<String, String> snapRenamedTable = + snap3.getMetadataManager().getSnapshotRenamedKeyTable(); + Table<String, RepeatedOmKeyInfo> snapDeletedTable = + snap3.getMetadataManager().getDeletedTable(); + + assertTableRowCount(snapRenamedTable, 3); + assertTableRowCount(snapDeletedDirTable, 2); + // All the keys deleted before snapshot2 is moved to snap3 Review Comment: Updated it. -- 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]
