swamirishi commented on code in PR #8392:
URL: https://github.com/apache/ozone/pull/8392#discussion_r2080178757


##########
hadoop-ozone/ozone-manager/src/test/java/org/apache/hadoop/ozone/om/snapshot/filter/TestReclaimableKeyFilter.java:
##########
@@ -0,0 +1,267 @@
+/*
+ * 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.junit.jupiter.api.Assertions.assertTrue;
+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 java.util.concurrent.atomic.AtomicLong;
+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.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.apache.hadoop.ozone.om.snapshot.SnapshotUtils;
+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 ReclaimableKeyFilter.
+ */
+public class TestReclaimableKeyFilter extends AbstractReclaimableFilterTest {
+  @Override
+  protected ReclaimableFilter initializeFilter(OzoneManager om, 
OmSnapshotManager snapshotManager,
+                                               SnapshotChainManager 
chainManager, SnapshotInfo currentSnapshotInfo,
+                                               KeyManager km, 
IOzoneManagerLock lock,
+                                               int 
numberOfPreviousSnapshotsFromChain) {
+    return new ReclaimableKeyFilter(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 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;
+  }
+
+  @SuppressWarnings("checkstyle:ParameterNumber")
+  private void testReclaimableKeyFilter(String volume, String bucket, int 
index,
+                                        OmKeyInfo keyInfo, OmKeyInfo 
prevKeyInfo, OmKeyInfo prevPrevKeyInfo,
+                                        Boolean expectedValue,
+                                        Optional<AtomicLong> size, 
Optional<AtomicLong> replicatedSize)
+      throws IOException {
+    List<SnapshotInfo> snapshotInfos = getLastSnapshotInfos(volume, bucket, 2, 
index);
+    SnapshotInfo previousToPreviousSapshotInfo = snapshotInfos.get(0);
+    SnapshotInfo prevSnapshotInfo = snapshotInfos.get(1);
+    OmBucketInfo bucketInfo = getOzoneManager().getBucketInfo(volume, bucket);
+    long volumeId = getOzoneManager().getMetadataManager().getVolumeId(volume);
+
+    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);
+    ReferenceCounted<OmSnapshot> prevToPrevSnap = 
Optional.ofNullable(previousToPreviousSapshotInfo)
+        .map(info -> {
+          try {
+            return getOmSnapshotManager().getActiveSnapshot(volume, bucket, 
info.getName());
+          } catch (IOException e) {
+            throw new RuntimeException(e);
+          }
+        }).orElse(null);
+
+    KeyManager keyManager = getKeyManager();
+    KeyManager prevKeyManager = mockOmSnapshot(prevSnap);
+    KeyManager prevToPrevKeyManager = mockOmSnapshot(prevToPrevSnap);
+    if (prevKeyManager != null) {
+      when(keyManager.getPreviousSnapshotOzoneKeyInfo(eq(volumeId),
+          eq(bucketInfo), eq(keyInfo)))
+          .thenReturn((km) -> prevKeyInfo);
+    }
+    if (prevKeyInfo != null && prevKeyManager != null && prevToPrevKeyManager 
!= null) {
+      when(prevKeyManager.getPreviousSnapshotOzoneKeyInfo(eq(volumeId),
+          eq(bucketInfo), eq(prevKeyInfo))).thenReturn((km) -> 
prevPrevKeyInfo);
+    }
+    when(keyInfo.getVolumeName()).thenReturn(volume);
+    when(keyInfo.getBucketName()).thenReturn(bucket);
+    assertEquals(expectedValue, 
getReclaimableFilter().apply(Table.newKeyValue("key", keyInfo)));
+    ReclaimableKeyFilter keyFilter = (ReclaimableKeyFilter) 
getReclaimableFilter();
+    if (prevSnap != null) {
+      assertEquals(size.map(AtomicLong::get).orElse(null),
+          keyFilter.getExclusiveSizeMap().get(prevSnap.get().getSnapshotID()));
+      assertEquals(replicatedSize.map(AtomicLong::get).orElse(null),
+          
keyFilter.getExclusiveReplicatedSizeMap().get(prevSnap.get().getSnapshotID()));
+    } else {
+      assertTrue(keyFilter.getExclusiveReplicatedSizeMap().isEmpty());
+      assertTrue(keyFilter.getExclusiveSizeMap().isEmpty());
+    }
+
+  }
+
+  private OmKeyInfo getMockedOmKeyInfo(long objectId, long size, long 
replicatedSize) {
+    OmKeyInfo keyInfo = mock(OmKeyInfo.class);
+    when(keyInfo.getObjectID()).thenReturn(objectId);
+    when(keyInfo.getDataSize()).thenReturn(size);
+    when(keyInfo.getReplicatedSize()).thenReturn(replicatedSize);
+    return keyInfo;
+  }
+
+  private OmKeyInfo getMockedOmKeyInfo(long objectId) {
+    return getMockedOmKeyInfo(objectId, 0, 0);
+  }
+

Review Comment:
   done



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