jojochuang commented on code in PR #8055:
URL: https://github.com/apache/ozone/pull/8055#discussion_r2074188156


##########
hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/ozone/om/TestKeyManagerImpl.java:
##########
@@ -1585,6 +1589,99 @@ void testGetNotExistedPart() throws IOException {
     assertEquals(0, locationList.size());
   }
 
+  private <T> Table<String, T> getMockedTable(Map<String, T> map) throws 
IOException {
+    Table<String, T> table = mock(Table.class);
+    when(table.get(anyString())).thenAnswer(i -> map.get(i.getArgument(0)));
+    when(table.getIfExist(anyString())).thenAnswer(i -> 
map.get(i.getArgument(0)));
+    return table;
+  }
+
+  private OmKeyInfo getMockedOmKeyInfo(OmBucketInfo bucketInfo, long parentId, 
String key, long objectId) {
+    OmKeyInfo omKeyInfo = mock(OmKeyInfo.class);
+    if (bucketInfo.getBucketLayout().isFileSystemOptimized()) {
+      when(omKeyInfo.getFileName()).thenReturn(key);
+    } else {
+      when(omKeyInfo.getParentObjectID()).thenReturn(parentId);
+      when(omKeyInfo.getKeyName()).thenReturn(key);
+    }
+    when(omKeyInfo.getObjectID()).thenReturn(objectId);
+    return omKeyInfo;
+  }
+
+  private OmDirectoryInfo getMockedOmDirInfo(long parentId, String key, long 
objectId) {
+    OmDirectoryInfo omKeyInfo = mock(OmDirectoryInfo.class);
+    when(omKeyInfo.getName()).thenReturn(key);
+    when(omKeyInfo.getParentObjectID()).thenReturn(parentId);
+    when(omKeyInfo.getParentObjectID()).thenReturn(0L);
+    when(omKeyInfo.getObjectID()).thenReturn(objectId);
+    return omKeyInfo;
+  }
+
+  private String getPath(long volumeId, OmBucketInfo bucketInfo, OmKeyInfo 
omKeyInfo) {
+    if (bucketInfo.getBucketLayout().isFileSystemOptimized()) {
+      return volumeId + "/" + bucketInfo.getObjectID() + "/" + 
omKeyInfo.getParentObjectID() + "/" +
+          omKeyInfo.getFileName();
+    } else {
+      return bucketInfo.getVolumeName() + "/" + bucketInfo.getBucketName() + 
"/" + omKeyInfo.getKeyName();
+    }
+  }
+
+  private String getPath(long volumeId, OmBucketInfo bucketInfo, 
OmDirectoryInfo omDirInfo) {

Review Comment:
   IMO  be renamed as "getDirectoryKey" or something else would be more 
readable.



##########
hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/snapshot/filter/ReclaimableDirFilter.java:
##########
@@ -0,0 +1,73 @@
+/*
+ * 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.filter;
+
+import java.io.IOException;
+import org.apache.hadoop.hdds.utils.db.Table;
+import org.apache.hadoop.ozone.om.KeyManager;
+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.SnapshotChainManager;
+import org.apache.hadoop.ozone.om.helpers.OmBucketInfo;
+import org.apache.hadoop.ozone.om.helpers.OmDirectoryInfo;
+import org.apache.hadoop.ozone.om.helpers.OmKeyInfo;
+import org.apache.hadoop.ozone.om.helpers.SnapshotInfo;
+import org.apache.hadoop.ozone.om.lock.IOzoneManagerLock;
+import org.apache.hadoop.ozone.om.snapshot.ReferenceCounted;
+
+/**
+ * Class to filter out deleted directories which are reclaimable based on 
their presence in previous snapshot in
+ * the snapshot chain.
+ */
+public class ReclaimableDirFilter extends ReclaimableFilter<OmKeyInfo> {
+
+  public ReclaimableDirFilter(OzoneManager ozoneManager,
+                              OmSnapshotManager omSnapshotManager, 
SnapshotChainManager snapshotChainManager,
+                              SnapshotInfo currentSnapshotInfo, KeyManager 
keyManager,
+                              IOzoneManagerLock lock) {
+    super(ozoneManager, omSnapshotManager, snapshotChainManager, 
currentSnapshotInfo, keyManager, lock, 1);
+  }
+
+  @Override
+  protected String getVolumeName(Table.KeyValue<String, OmKeyInfo> keyValue) 
throws IOException {
+    return keyValue.getValue().getVolumeName();
+  }
+
+  @Override
+  protected String getBucketName(Table.KeyValue<String, OmKeyInfo> keyValue) 
throws IOException {
+    return keyValue.getValue().getBucketName();
+  }
+
+  @Override
+  protected Boolean isReclaimable(Table.KeyValue<String, OmKeyInfo> 
deletedDirInfo) throws IOException {
+    ReferenceCounted<OmSnapshot> previousSnapshot = getPreviousOmSnapshot(0);
+    KeyManager prevKeyManager = previousSnapshot == null ? null : 
previousSnapshot.get().getKeyManager();
+    return isDirReclaimable(getVolumeId(), getBucketInfo(), 
deletedDirInfo.getValue(), getKeyManager(), prevKeyManager);
+  }
+
+  private boolean isDirReclaimable(long volumeId, OmBucketInfo bucketInfo, 
OmKeyInfo dirInfo,
+                                   KeyManager keyManager, KeyManager 
previousKeyManager) throws IOException {
+    if (previousKeyManager == null) {
+      return true;
+    }
+    OmDirectoryInfo prevDirectoryInfo =
+        keyManager.getPreviousSnapshotOzoneDirInfo(volumeId, bucketInfo, 
dirInfo).apply(previousKeyManager);
+    return prevDirectoryInfo == null || prevDirectoryInfo.getObjectID() != 
dirInfo.getObjectID();

Review Comment:
   if the directory object is not in the previous snapshot, or the object id is 
not the same (the directory was deleted and then a new one was created), then 
it is reclaimable.



##########
hadoop-ozone/ozone-manager/src/test/java/org/apache/hadoop/ozone/om/snapshot/filter/TestReclaimableDirFilter.java:
##########
@@ -0,0 +1,142 @@
+/*
+ * 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.filter;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Optional;
+import org.apache.hadoop.hdds.utils.db.Table;
+import org.apache.hadoop.ozone.om.KeyManager;
+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.SnapshotChainManager;
+import org.apache.hadoop.ozone.om.helpers.OmBucketInfo;
+import org.apache.hadoop.ozone.om.helpers.OmDirectoryInfo;
+import org.apache.hadoop.ozone.om.helpers.OmKeyInfo;
+import org.apache.hadoop.ozone.om.helpers.SnapshotInfo;
+import org.apache.hadoop.ozone.om.lock.IOzoneManagerLock;
+import org.apache.hadoop.ozone.om.snapshot.ReferenceCounted;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+import org.rocksdb.RocksDBException;
+
+/**
+ * Test class for ReclaimableDirFilter.
+ */
+public class TestReclaimableDirFilter extends AbstractReclaimableFilterTest {
+  @Override
+  protected ReclaimableFilter initializeFilter(OzoneManager om, 
OmSnapshotManager snapshotManager,
+                                               SnapshotChainManager 
chainManager, SnapshotInfo currentSnapshotInfo,
+                                               KeyManager km, 
IOzoneManagerLock lock,
+                                               int 
numberOfPreviousSnapshotsFromChain) {
+    return new ReclaimableDirFilter(om, snapshotManager, chainManager, 
currentSnapshotInfo, km, lock);
+  }
+
+  List<Arguments> testReclaimableFilterArguments() {
+    List<Arguments> arguments = new ArrayList<>();
+    for (int i = 0; i < 3; i++) {
+      for (int j = 0; j < 5; j++) {
+        arguments.add(Arguments.of(i, j));
+      }
+    }
+    return arguments;
+  }
+
+  private void testReclaimableDirFilter(String volume, String bucket, int 
index,
+                                        OmKeyInfo dirInfo, OmDirectoryInfo 
prevDirInfo,
+                                        Boolean expectedValue)
+      throws IOException {
+    List<SnapshotInfo> snapshotInfos = getLastSnapshotInfos(volume, bucket, 1, 
index);
+    SnapshotInfo prevSnapshotInfo = snapshotInfos.get(0);
+    OmBucketInfo bucketInfo = getOzoneManager().getBucketInfo(volume, bucket);
+    long volumeId = getOzoneManager().getMetadataManager().getVolumeId(volume);
+    KeyManager keyManager = getKeyManager();
+    if (prevSnapshotInfo != null) {
+      ReferenceCounted<OmSnapshot> prevSnap = 
Optional.ofNullable(prevSnapshotInfo)
+          .map(info -> {
+            try {
+              return getOmSnapshotManager().getActiveSnapshot(volume, bucket, 
info.getName());
+            } catch (IOException e) {
+              throw new RuntimeException(e);
+            }
+          }).orElse(null);
+      mockOmSnapshot(prevSnap);
+      when(keyManager.getPreviousSnapshotOzoneDirInfo(eq(volumeId), 
eq(bucketInfo), eq(dirInfo)))
+          .thenReturn((km) -> prevDirInfo);
+    }
+
+    when(dirInfo.getVolumeName()).thenReturn(volume);
+    when(dirInfo.getBucketName()).thenReturn(bucket);
+    assertEquals(expectedValue, 
getReclaimableFilter().apply(Table.newKeyValue("key", dirInfo)));
+  }
+
+  private OmKeyInfo getMockedOmKeyInfo(long objectId) {
+    OmKeyInfo keyInfo = mock(OmKeyInfo.class);
+    when(keyInfo.getObjectID()).thenReturn(objectId);
+    return keyInfo;
+  }
+
+  private OmDirectoryInfo getMockedOmDirInfo(long objectId) {
+    OmDirectoryInfo keyInfo = mock(OmDirectoryInfo.class);
+    when(keyInfo.getObjectID()).thenReturn(objectId);
+    return keyInfo;
+  }
+
+  private KeyManager mockOmSnapshot(ReferenceCounted<OmSnapshot> snapshot) {
+    if (snapshot != null) {
+      OmSnapshot omSnapshot = snapshot.get();
+      KeyManager keyManager = mock(KeyManager.class);
+      when(omSnapshot.getKeyManager()).thenReturn(keyManager);
+      return keyManager;
+    }
+    return null;
+  }
+
+  @ParameterizedTest
+  @MethodSource("testReclaimableFilterArguments")
+  public void testNonReclaimableDirectory(int actualNumberOfSnapshots, int 
index) throws IOException, RocksDBException {
+    setup(1, actualNumberOfSnapshots, index, 4, 2);
+    String volume = getVolumes().get(3);
+    String bucket = getBuckets().get(1);
+    index = Math.min(index, actualNumberOfSnapshots);
+    OmKeyInfo dirInfo = getMockedOmKeyInfo(1);
+    OmDirectoryInfo prevDirectoryInfo = index - 1 >= 0 ? getMockedOmDirInfo(1) 
: null;
+    testReclaimableDirFilter(volume, bucket, index, dirInfo, 
prevDirectoryInfo, prevDirectoryInfo == null);
+  }
+
+  @ParameterizedTest
+  @MethodSource("testReclaimableFilterArguments")
+  public void testReclaimableKeyWithDifferentObjId(int 
actualNumberOfSnapshots, int index)
+      throws IOException, RocksDBException {
+    setup(1, actualNumberOfSnapshots, index, 4, 2);
+    String volume = getVolumes().get(3);
+    String bucket = getBuckets().get(1);
+    index = Math.min(index, actualNumberOfSnapshots);
+    OmKeyInfo dirInfo = getMockedOmKeyInfo(1);
+    OmDirectoryInfo prevDirectoryInfo = index - 1 >= 0 ? getMockedOmDirInfo(2) 
: null;
+    testReclaimableDirFilter(volume, bucket, index, dirInfo, 
prevDirectoryInfo, true);

Review Comment:
   the case where the same directory object is in the previous snapshot.



##########
hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/ozone/om/TestKeyManagerImpl.java:
##########
@@ -1585,6 +1589,99 @@ void testGetNotExistedPart() throws IOException {
     assertEquals(0, locationList.size());
   }
 
+  private <T> Table<String, T> getMockedTable(Map<String, T> map) throws 
IOException {
+    Table<String, T> table = mock(Table.class);
+    when(table.get(anyString())).thenAnswer(i -> map.get(i.getArgument(0)));
+    when(table.getIfExist(anyString())).thenAnswer(i -> 
map.get(i.getArgument(0)));
+    return table;
+  }
+
+  private OmKeyInfo getMockedOmKeyInfo(OmBucketInfo bucketInfo, long parentId, 
String key, long objectId) {
+    OmKeyInfo omKeyInfo = mock(OmKeyInfo.class);
+    if (bucketInfo.getBucketLayout().isFileSystemOptimized()) {
+      when(omKeyInfo.getFileName()).thenReturn(key);
+    } else {
+      when(omKeyInfo.getParentObjectID()).thenReturn(parentId);
+      when(omKeyInfo.getKeyName()).thenReturn(key);
+    }
+    when(omKeyInfo.getObjectID()).thenReturn(objectId);
+    return omKeyInfo;
+  }
+
+  private OmDirectoryInfo getMockedOmDirInfo(long parentId, String key, long 
objectId) {
+    OmDirectoryInfo omKeyInfo = mock(OmDirectoryInfo.class);
+    when(omKeyInfo.getName()).thenReturn(key);
+    when(omKeyInfo.getParentObjectID()).thenReturn(parentId);

Review Comment:
   is this line redundant?



##########
hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/snapshot/filter/ReclaimableDirFilter.java:
##########
@@ -0,0 +1,73 @@
+/*
+ * 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.filter;
+
+import java.io.IOException;
+import org.apache.hadoop.hdds.utils.db.Table;
+import org.apache.hadoop.ozone.om.KeyManager;
+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.SnapshotChainManager;
+import org.apache.hadoop.ozone.om.helpers.OmBucketInfo;
+import org.apache.hadoop.ozone.om.helpers.OmDirectoryInfo;
+import org.apache.hadoop.ozone.om.helpers.OmKeyInfo;
+import org.apache.hadoop.ozone.om.helpers.SnapshotInfo;
+import org.apache.hadoop.ozone.om.lock.IOzoneManagerLock;
+import org.apache.hadoop.ozone.om.snapshot.ReferenceCounted;
+
+/**
+ * Class to filter out deleted directories which are reclaimable based on 
their presence in previous snapshot in
+ * the snapshot chain.
+ */
+public class ReclaimableDirFilter extends ReclaimableFilter<OmKeyInfo> {
+
+  public ReclaimableDirFilter(OzoneManager ozoneManager,
+                              OmSnapshotManager omSnapshotManager, 
SnapshotChainManager snapshotChainManager,
+                              SnapshotInfo currentSnapshotInfo, KeyManager 
keyManager,
+                              IOzoneManagerLock lock) {
+    super(ozoneManager, omSnapshotManager, snapshotChainManager, 
currentSnapshotInfo, keyManager, lock, 1);
+  }
+
+  @Override
+  protected String getVolumeName(Table.KeyValue<String, OmKeyInfo> keyValue) 
throws IOException {
+    return keyValue.getValue().getVolumeName();
+  }
+
+  @Override
+  protected String getBucketName(Table.KeyValue<String, OmKeyInfo> keyValue) 
throws IOException {
+    return keyValue.getValue().getBucketName();
+  }
+
+  @Override
+  protected Boolean isReclaimable(Table.KeyValue<String, OmKeyInfo> 
deletedDirInfo) throws IOException {
+    ReferenceCounted<OmSnapshot> previousSnapshot = getPreviousOmSnapshot(0);

Review Comment:
   ReclaimableFilter.initializePreviousSnapshotsFromChain() should have an 
assert that previousOmSnapshots is exactly numberOfPreviousSnapshotsFromChain 
snapshots.



##########
hadoop-ozone/ozone-manager/src/test/java/org/apache/hadoop/ozone/om/snapshot/filter/TestReclaimableDirFilter.java:
##########
@@ -0,0 +1,142 @@
+/*
+ * 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.filter;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Optional;
+import org.apache.hadoop.hdds.utils.db.Table;
+import org.apache.hadoop.ozone.om.KeyManager;
+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.SnapshotChainManager;
+import org.apache.hadoop.ozone.om.helpers.OmBucketInfo;
+import org.apache.hadoop.ozone.om.helpers.OmDirectoryInfo;
+import org.apache.hadoop.ozone.om.helpers.OmKeyInfo;
+import org.apache.hadoop.ozone.om.helpers.SnapshotInfo;
+import org.apache.hadoop.ozone.om.lock.IOzoneManagerLock;
+import org.apache.hadoop.ozone.om.snapshot.ReferenceCounted;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+import org.rocksdb.RocksDBException;
+
+/**
+ * Test class for ReclaimableDirFilter.
+ */
+public class TestReclaimableDirFilter extends AbstractReclaimableFilterTest {
+  @Override
+  protected ReclaimableFilter initializeFilter(OzoneManager om, 
OmSnapshotManager snapshotManager,
+                                               SnapshotChainManager 
chainManager, SnapshotInfo currentSnapshotInfo,
+                                               KeyManager km, 
IOzoneManagerLock lock,
+                                               int 
numberOfPreviousSnapshotsFromChain) {
+    return new ReclaimableDirFilter(om, snapshotManager, chainManager, 
currentSnapshotInfo, km, lock);
+  }
+
+  List<Arguments> testReclaimableFilterArguments() {
+    List<Arguments> arguments = new ArrayList<>();
+    for (int i = 0; i < 3; i++) {
+      for (int j = 0; j < 5; j++) {
+        arguments.add(Arguments.of(i, j));
+      }
+    }
+    return arguments;
+  }
+
+  private void testReclaimableDirFilter(String volume, String bucket, int 
index,
+                                        OmKeyInfo dirInfo, OmDirectoryInfo 
prevDirInfo,
+                                        Boolean expectedValue)
+      throws IOException {
+    List<SnapshotInfo> snapshotInfos = getLastSnapshotInfos(volume, bucket, 1, 
index);
+    SnapshotInfo prevSnapshotInfo = snapshotInfos.get(0);
+    OmBucketInfo bucketInfo = getOzoneManager().getBucketInfo(volume, bucket);
+    long volumeId = getOzoneManager().getMetadataManager().getVolumeId(volume);
+    KeyManager keyManager = getKeyManager();
+    if (prevSnapshotInfo != null) {
+      ReferenceCounted<OmSnapshot> prevSnap = 
Optional.ofNullable(prevSnapshotInfo)
+          .map(info -> {
+            try {
+              return getOmSnapshotManager().getActiveSnapshot(volume, bucket, 
info.getName());
+            } catch (IOException e) {
+              throw new RuntimeException(e);
+            }
+          }).orElse(null);
+      mockOmSnapshot(prevSnap);
+      when(keyManager.getPreviousSnapshotOzoneDirInfo(eq(volumeId), 
eq(bucketInfo), eq(dirInfo)))
+          .thenReturn((km) -> prevDirInfo);
+    }
+
+    when(dirInfo.getVolumeName()).thenReturn(volume);
+    when(dirInfo.getBucketName()).thenReturn(bucket);
+    assertEquals(expectedValue, 
getReclaimableFilter().apply(Table.newKeyValue("key", dirInfo)));
+  }
+
+  private OmKeyInfo getMockedOmKeyInfo(long objectId) {
+    OmKeyInfo keyInfo = mock(OmKeyInfo.class);
+    when(keyInfo.getObjectID()).thenReturn(objectId);
+    return keyInfo;
+  }
+
+  private OmDirectoryInfo getMockedOmDirInfo(long objectId) {
+    OmDirectoryInfo keyInfo = mock(OmDirectoryInfo.class);
+    when(keyInfo.getObjectID()).thenReturn(objectId);
+    return keyInfo;
+  }
+
+  private KeyManager mockOmSnapshot(ReferenceCounted<OmSnapshot> snapshot) {
+    if (snapshot != null) {
+      OmSnapshot omSnapshot = snapshot.get();
+      KeyManager keyManager = mock(KeyManager.class);
+      when(omSnapshot.getKeyManager()).thenReturn(keyManager);
+      return keyManager;
+    }
+    return null;
+  }
+
+  @ParameterizedTest
+  @MethodSource("testReclaimableFilterArguments")
+  public void testNonReclaimableDirectory(int actualNumberOfSnapshots, int 
index) throws IOException, RocksDBException {
+    setup(1, actualNumberOfSnapshots, index, 4, 2);
+    String volume = getVolumes().get(3);
+    String bucket = getBuckets().get(1);
+    index = Math.min(index, actualNumberOfSnapshots);
+    OmKeyInfo dirInfo = getMockedOmKeyInfo(1);
+    OmDirectoryInfo prevDirectoryInfo = index - 1 >= 0 ? getMockedOmDirInfo(1) 
: null;
+    testReclaimableDirFilter(volume, bucket, index, dirInfo, 
prevDirectoryInfo, prevDirectoryInfo == null);
+  }
+
+  @ParameterizedTest
+  @MethodSource("testReclaimableFilterArguments")
+  public void testReclaimableKeyWithDifferentObjId(int 
actualNumberOfSnapshots, int index)
+      throws IOException, RocksDBException {
+    setup(1, actualNumberOfSnapshots, index, 4, 2);
+    String volume = getVolumes().get(3);
+    String bucket = getBuckets().get(1);
+    index = Math.min(index, actualNumberOfSnapshots);
+    OmKeyInfo dirInfo = getMockedOmKeyInfo(1);
+    OmDirectoryInfo prevDirectoryInfo = index - 1 >= 0 ? getMockedOmDirInfo(2) 
: null;
+    testReclaimableDirFilter(volume, bucket, index, dirInfo, 
prevDirectoryInfo, true);

Review Comment:
   should we also check the case where a different directory object is in the 
previous snapshot? In which case it should be reclaimable.



##########
hadoop-ozone/ozone-manager/src/test/java/org/apache/hadoop/ozone/om/snapshot/filter/TestReclaimableDirFilter.java:
##########
@@ -0,0 +1,142 @@
+/*
+ * 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.filter;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Optional;
+import org.apache.hadoop.hdds.utils.db.Table;
+import org.apache.hadoop.ozone.om.KeyManager;
+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.SnapshotChainManager;
+import org.apache.hadoop.ozone.om.helpers.OmBucketInfo;
+import org.apache.hadoop.ozone.om.helpers.OmDirectoryInfo;
+import org.apache.hadoop.ozone.om.helpers.OmKeyInfo;
+import org.apache.hadoop.ozone.om.helpers.SnapshotInfo;
+import org.apache.hadoop.ozone.om.lock.IOzoneManagerLock;
+import org.apache.hadoop.ozone.om.snapshot.ReferenceCounted;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+import org.rocksdb.RocksDBException;
+
+/**
+ * Test class for ReclaimableDirFilter.
+ */
+public class TestReclaimableDirFilter extends AbstractReclaimableFilterTest {
+  @Override
+  protected ReclaimableFilter initializeFilter(OzoneManager om, 
OmSnapshotManager snapshotManager,
+                                               SnapshotChainManager 
chainManager, SnapshotInfo currentSnapshotInfo,
+                                               KeyManager km, 
IOzoneManagerLock lock,
+                                               int 
numberOfPreviousSnapshotsFromChain) {
+    return new ReclaimableDirFilter(om, snapshotManager, chainManager, 
currentSnapshotInfo, km, lock);
+  }
+
+  List<Arguments> testReclaimableFilterArguments() {
+    List<Arguments> arguments = new ArrayList<>();
+    for (int i = 0; i < 3; i++) {
+      for (int j = 0; j < 5; j++) {
+        arguments.add(Arguments.of(i, j));
+      }
+    }
+    return arguments;
+  }
+
+  private void testReclaimableDirFilter(String volume, String bucket, int 
index,
+                                        OmKeyInfo dirInfo, OmDirectoryInfo 
prevDirInfo,
+                                        Boolean expectedValue)
+      throws IOException {
+    List<SnapshotInfo> snapshotInfos = getLastSnapshotInfos(volume, bucket, 1, 
index);
+    SnapshotInfo prevSnapshotInfo = snapshotInfos.get(0);
+    OmBucketInfo bucketInfo = getOzoneManager().getBucketInfo(volume, bucket);
+    long volumeId = getOzoneManager().getMetadataManager().getVolumeId(volume);
+    KeyManager keyManager = getKeyManager();
+    if (prevSnapshotInfo != null) {
+      ReferenceCounted<OmSnapshot> prevSnap = 
Optional.ofNullable(prevSnapshotInfo)
+          .map(info -> {
+            try {
+              return getOmSnapshotManager().getActiveSnapshot(volume, bucket, 
info.getName());
+            } catch (IOException e) {
+              throw new RuntimeException(e);
+            }
+          }).orElse(null);
+      mockOmSnapshot(prevSnap);
+      when(keyManager.getPreviousSnapshotOzoneDirInfo(eq(volumeId), 
eq(bucketInfo), eq(dirInfo)))
+          .thenReturn((km) -> prevDirInfo);
+    }
+
+    when(dirInfo.getVolumeName()).thenReturn(volume);
+    when(dirInfo.getBucketName()).thenReturn(bucket);
+    assertEquals(expectedValue, 
getReclaimableFilter().apply(Table.newKeyValue("key", dirInfo)));
+  }
+
+  private OmKeyInfo getMockedOmKeyInfo(long objectId) {
+    OmKeyInfo keyInfo = mock(OmKeyInfo.class);
+    when(keyInfo.getObjectID()).thenReturn(objectId);
+    return keyInfo;
+  }
+
+  private OmDirectoryInfo getMockedOmDirInfo(long objectId) {
+    OmDirectoryInfo keyInfo = mock(OmDirectoryInfo.class);
+    when(keyInfo.getObjectID()).thenReturn(objectId);
+    return keyInfo;
+  }
+
+  private KeyManager mockOmSnapshot(ReferenceCounted<OmSnapshot> snapshot) {
+    if (snapshot != null) {
+      OmSnapshot omSnapshot = snapshot.get();
+      KeyManager keyManager = mock(KeyManager.class);
+      when(omSnapshot.getKeyManager()).thenReturn(keyManager);
+      return keyManager;
+    }
+    return null;
+  }
+
+  @ParameterizedTest
+  @MethodSource("testReclaimableFilterArguments")
+  public void testNonReclaimableDirectory(int actualNumberOfSnapshots, int 
index) throws IOException, RocksDBException {
+    setup(1, actualNumberOfSnapshots, index, 4, 2);
+    String volume = getVolumes().get(3);
+    String bucket = getBuckets().get(1);
+    index = Math.min(index, actualNumberOfSnapshots);
+    OmKeyInfo dirInfo = getMockedOmKeyInfo(1);
+    OmDirectoryInfo prevDirectoryInfo = index - 1 >= 0 ? getMockedOmDirInfo(1) 
: null;
+    testReclaimableDirFilter(volume, bucket, index, dirInfo, 
prevDirectoryInfo, prevDirectoryInfo == null);

Review Comment:
   the case where the directory is not in the previous snapshot.



##########
hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/ozone/om/TestKeyManagerImpl.java:
##########
@@ -1585,6 +1589,99 @@ void testGetNotExistedPart() throws IOException {
     assertEquals(0, locationList.size());
   }
 
+  private <T> Table<String, T> getMockedTable(Map<String, T> map) throws 
IOException {
+    Table<String, T> table = mock(Table.class);
+    when(table.get(anyString())).thenAnswer(i -> map.get(i.getArgument(0)));
+    when(table.getIfExist(anyString())).thenAnswer(i -> 
map.get(i.getArgument(0)));
+    return table;
+  }
+
+  private OmKeyInfo getMockedOmKeyInfo(OmBucketInfo bucketInfo, long parentId, 
String key, long objectId) {
+    OmKeyInfo omKeyInfo = mock(OmKeyInfo.class);
+    if (bucketInfo.getBucketLayout().isFileSystemOptimized()) {
+      when(omKeyInfo.getFileName()).thenReturn(key);
+    } else {
+      when(omKeyInfo.getParentObjectID()).thenReturn(parentId);
+      when(omKeyInfo.getKeyName()).thenReturn(key);
+    }
+    when(omKeyInfo.getObjectID()).thenReturn(objectId);
+    return omKeyInfo;
+  }
+
+  private OmDirectoryInfo getMockedOmDirInfo(long parentId, String key, long 
objectId) {
+    OmDirectoryInfo omKeyInfo = mock(OmDirectoryInfo.class);
+    when(omKeyInfo.getName()).thenReturn(key);
+    when(omKeyInfo.getParentObjectID()).thenReturn(parentId);

Review Comment:
   this line will not take effect as it will be replaced by the next line.



-- 
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]

Reply via email to