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


##########
hadoop-ozone/common/src/test/java/org/apache/hadoop/ozone/om/lock/TestPoolBasedHierarchicalResourceLockManager.java:
##########
@@ -0,0 +1,577 @@
+/*
+ * 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.lock;
+
+import static 
org.apache.hadoop.ozone.om.OMConfigKeys.OZONE_OM_HIERARCHICAL_RESOURCE_LOCKS_HARD_LIMIT;
+import static 
org.apache.hadoop.ozone.om.OMConfigKeys.OZONE_OM_HIERARCHICAL_RESOURCE_LOCKS_HARD_LIMIT_DEFAULT;
+import static 
org.apache.hadoop.ozone.om.OMConfigKeys.OZONE_OM_HIERARCHICAL_RESOURCE_LOCKS_SOFT_LIMIT;
+import static 
org.apache.hadoop.ozone.om.OMConfigKeys.OZONE_OM_HIERARCHICAL_RESOURCE_LOCKS_SOFT_LIMIT_DEFAULT;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assertions.fail;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.atomic.AtomicReference;
+import org.apache.hadoop.hdds.conf.OzoneConfiguration;
+import 
org.apache.hadoop.ozone.om.lock.HierachicalResourceLockManager.HierarchicalResourceLock;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.Timeout;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.EnumSource;
+import org.junit.jupiter.params.provider.ValueSource;
+
+/**
+ * Test class for {@link PoolBasedHierarchicalResourceLockManager}.
+ *
+ * This class tests the functionality of the pool-based hierarchical resource 
lock manager,
+ * including basic lock operations, concurrency scenarios, resource pool 
management,
+ * and error conditions.
+ */
+public class TestPoolBasedHierarchicalResourceLockManager {
+
+  private PoolBasedHierarchicalResourceLockManager lockManager;
+
+  @BeforeEach
+  public void setUp() {
+    OzoneConfiguration conf = new OzoneConfiguration();
+    lockManager = new PoolBasedHierarchicalResourceLockManager(conf);
+  }
+
+  @AfterEach
+  public void tearDown() {
+    if (lockManager != null) {
+      lockManager.close();
+    }
+  }
+
+  /**
+   * Test basic read lock acquisition and release.
+   */
+  @Test
+  public void testBasicReadLockAcquisition() throws Exception {
+    String key = "test-key-1";
+
+    try (HierarchicalResourceLock lock = 
lockManager.acquireReadLock(FlatResource.SNAPSHOT_GC_LOCK, key)) {
+      assertNotNull(lock);
+      assertTrue(lock.isLockAcquired());
+    }
+  }
+
+  /**
+   * Test basic write lock acquisition and release.
+   */
+  @Test
+  public void testBasicWriteLockAcquisition() throws Exception {
+    String key = "test-key-2";
+
+    try (HierarchicalResourceLock lock = 
lockManager.acquireWriteLock(FlatResource.SNAPSHOT_DB_LOCK, key)) {
+      assertNotNull(lock);
+      assertTrue(lock.isLockAcquired());
+    }
+  }
+
+  /**
+   * Test multiple read locks can be acquired on the same resource.
+   */
+  @Test
+  public void testMultipleReadLocks() throws Exception {
+    String key = "test-key-3";
+
+    try (HierarchicalResourceLock lock1 = 
lockManager.acquireReadLock(FlatResource.SNAPSHOT_GC_LOCK, key);
+         HierarchicalResourceLock lock2 = 
lockManager.acquireReadLock(FlatResource.SNAPSHOT_GC_LOCK, key)) {
+
+      assertNotNull(lock1);
+      assertNotNull(lock2);
+      assertTrue(lock1.isLockAcquired());
+      assertTrue(lock2.isLockAcquired());
+    }
+  }
+
+  /**
+   * Test write lock exclusivity - only one write lock can be acquired at a 
time.
+   */
+  @Test
+  @Timeout(10)
+  public void testWriteLockExclusivity() throws Exception {
+    String key = "test-key-4";
+    CountDownLatch latch1 = new CountDownLatch(1);
+    CountDownLatch latch2 = new CountDownLatch(1);
+    AtomicBoolean secondLockAcquired = new AtomicBoolean(false);
+
+    ExecutorService executor = Executors.newFixedThreadPool(2);
+
+    try {
+      // First thread acquires write lock
+      CompletableFuture<Void> future1 = CompletableFuture.runAsync(() -> {
+        try (HierarchicalResourceLock lock = 
lockManager.acquireWriteLock(FlatResource.SNAPSHOT_DB_LOCK, key)) {
+          latch1.countDown();
+          // Hold lock for a short time
+          Thread.sleep(100);
+        } catch (Exception e) {
+          fail("First thread failed to acquire lock: " + e.getMessage());
+        }
+      }, executor);
+
+      // Wait for first lock to be acquired
+      latch1.await();
+
+      // Second thread tries to acquire write lock
+      CompletableFuture<Void> future2 = CompletableFuture.runAsync(() -> {
+        try (HierarchicalResourceLock lock = 
lockManager.acquireWriteLock(FlatResource.SNAPSHOT_DB_LOCK, key)) {
+          secondLockAcquired.set(true);
+          latch2.countDown();
+        } catch (Exception e) {
+          fail("Second thread failed to acquire lock: " + e.getMessage());
+        }
+      }, executor);
+
+      // Wait for both threads to complete
+      future1.get(5, TimeUnit.SECONDS);
+      future2.get(5, TimeUnit.SECONDS);
+
+      // Second lock should have been acquired after first was released
+      assertTrue(secondLockAcquired.get());
+
+    } finally {
+      executor.shutdown();
+    }
+  }
+
+  /**
+   * Test read-write lock interaction - write lock blocks read locks.
+   */
+  @Test
+  @Timeout(10)
+  public void testReadWriteLockInteraction() throws Exception {
+    String key = "test-key-5";
+    CountDownLatch writeLockAcquired = new CountDownLatch(1);
+    CountDownLatch readLockAcquired = new CountDownLatch(1);
+    AtomicBoolean readLockBlocked = new AtomicBoolean(false);
+
+    ExecutorService executor = Executors.newFixedThreadPool(2);
+
+    try {
+      // First thread acquires write lock
+      CompletableFuture<Void> future1 = CompletableFuture.runAsync(() -> {
+        try (HierarchicalResourceLock lock = 
lockManager.acquireWriteLock(FlatResource.SNAPSHOT_GC_LOCK, key)) {
+          writeLockAcquired.countDown();
+          // Hold lock for a short time
+          Thread.sleep(200);
+        } catch (Exception e) {
+          fail("Write lock acquisition failed: " + e.getMessage());
+        }
+      }, executor);
+
+      // Wait for write lock to be acquired
+      writeLockAcquired.await();
+
+      // Second thread tries to acquire read lock
+      CompletableFuture<Void> future2 = CompletableFuture.runAsync(() -> {
+        try {
+          // This should block until write lock is released
+          readLockBlocked.set(true);
+          try (HierarchicalResourceLock lock = 
lockManager.acquireReadLock(FlatResource.SNAPSHOT_GC_LOCK, key)) {
+            readLockAcquired.countDown();
+          }
+        } catch (Exception e) {
+          fail("Read lock acquisition failed: " + e.getMessage());
+        }
+      }, executor);
+
+      // Wait for both threads to complete
+      future1.get(5, TimeUnit.SECONDS);
+      future2.get(5, TimeUnit.SECONDS);
+
+      assertTrue(readLockBlocked.get());
+      assertEquals(0, readLockAcquired.getCount());
+
+    } finally {
+      executor.shutdown();
+    }
+  }
+
+  /**
+   * Test lock state after closing.
+   */
+  @Test
+  public void testLockStateAfterClose() throws Exception {
+    String key = "test-key-6";
+
+    HierarchicalResourceLock lock = 
lockManager.acquireReadLock(FlatResource.SNAPSHOT_DB_LOCK, key);
+    assertTrue(lock.isLockAcquired());
+
+    lock.close();
+    assertFalse(lock.isLockAcquired());
+  }
+
+  /**
+   * Test double close doesn't cause issues.
+   */
+  @Test
+  public void testDoubleClose() throws Exception {
+    String key = "test-key-7";
+
+    HierarchicalResourceLock lock = 
lockManager.acquireWriteLock(FlatResource.SNAPSHOT_GC_LOCK, key);
+    assertTrue(lock.isLockAcquired());
+
+    // First close
+    lock.close();
+    assertFalse(lock.isLockAcquired());
+
+    // Second close should not throw exception
+    lock.close();
+    assertFalse(lock.isLockAcquired());
+  }
+
+  /**
+   * Test different resource types can be locked independently.
+   */
+  @ParameterizedTest
+  @EnumSource(FlatResource.class)
+  public void testDifferentResourceTypes(FlatResource resource) throws 
Exception {
+    String key = "test-key-" + resource.name();
+
+    try (HierarchicalResourceLock lock = 
lockManager.acquireWriteLock(resource, key)) {
+      assertNotNull(lock);
+      assertTrue(lock.isLockAcquired());
+    }
+  }

Review Comment:
   this test doesn't seem to do what it supposed to do.
   Instead, it should verify:
   
       try (HierarchicalResourceLock lock = 
lockManager.acquireWriteLock(SNAPSHOT_GC_LOCK, key)) {
         assertNotNull(lock);
         assertTrue(lock.isLockAcquired());
   
         try (HierarchicalResourceLock lock2 = 
lockManager.acquireWriteLock(SNAPSHOT_DB_LOCK, key)) {
           assertNotNull(lock2);
           assertTrue(lock2.isLockAcquired());
         }
       }
   
   so that it ensures the two locks can be held independently at the same time.



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