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


##########
hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/om/lock/OzoneManagerLock.java:
##########
@@ -581,151 +577,6 @@ public OMLockMetrics getOMLockMetrics() {
     return omLockMetrics;
   }
 
-  private abstract static class ResourceLockManager<T extends Resource> {

Review Comment:
   Basically, rename ResourceLockManager to ResourceLockTracker, and move it 
out to its own class file, so that it can be used by 
HierarchicalResourceLockManager.
   
   Similarly, DAGResourceLockManager renamed and moved to 
DAGResourceLockTracker.java



##########
hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/om/lock/DAGResourceLockTracker.java:
##########
@@ -0,0 +1,127 @@
+/*
+ * 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 java.util.Collections;
+import java.util.EnumMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.Stack;
+import java.util.stream.Stream;
+
+final class DAGResourceLockTracker extends 
ResourceLockTracker<DAGLeveledResource> {

Review Comment:
   Please add a javadoc:
   
   track lock usage and enforce a locking rule based on a DAG (directed acyclic 
graph) of resources: a resource should not be locked while any of its dependent 
resources (children or descendants in the DAG) are currently locked.



##########
hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/om/lock/DAGResourceLockTracker.java:
##########
@@ -0,0 +1,127 @@
+/*
+ * 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 java.util.Collections;
+import java.util.EnumMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.Stack;
+import java.util.stream.Stream;
+
+final class DAGResourceLockTracker extends 
ResourceLockTracker<DAGLeveledResource> {
+
+  private final EnumMap<DAGLeveledResource, ThreadLocal<Integer>> 
acquiredLocksMap =
+      new EnumMap<>(DAGLeveledResource.class);
+  private final Map<DAGLeveledResource, Set<DAGLeveledResource>> 
lockDependentAdjacencySet =
+      new EnumMap<>(DAGLeveledResource.class);
+
+  private static volatile DAGResourceLockTracker instance = null;
+
+  public static DAGResourceLockTracker get() {
+    if (instance == null) {
+      synchronized (DAGResourceLockTracker.class) {
+        if (instance == null) {
+          instance = new DAGResourceLockTracker();
+        }
+      }
+    }
+    return instance;
+  }
+

Review Comment:
       /**
        * Performs a depth-first traversal (DFS) on the directed acyclic graph 
(DAG)
        * of {@link DAGLeveledResource} objects, processing dependencies and 
updating
        * the lock-dependent adjacency set with resource relationships.
        *
        * @param resource the DAGLeveledResource instance to start the 
traversal from
        * @param stack a stack to manage the DFS traversal state
        * @param visited a set to keep track of resources that have already 
been visited
        *                during the traversal
        */



##########
hadoop-ozone/common/src/test/java/org/apache/hadoop/ozone/om/lock/TestPoolBasedHierarchicalResourceLockManager.java:
##########
@@ -580,4 +572,39 @@ public void testIOExceptionPropagation() {
       assertNotNull(e.getMessage());
     }
   }
+
+  @ParameterizedTest
+  @EnumSource
+  public void testDAGLockOrderAcquisition(DAGLeveledResource 
dagLeveledResource) throws IOException {
+    Map<DAGLeveledResource, Set<IOzoneManagerLock.Resource>> 
forbiddenLockOrdering =
+        ImmutableMap.of(SNAPSHOT_DB_CONTENT_LOCK, 
ImmutableSet.of(SNAPSHOT_DB_LOCK, SNAPSHOT_LOCAL_DATA_LOCK),
+            BOOTSTRAP_LOCK, ImmutableSet.of(SNAPSHOT_GC_LOCK, 
SNAPSHOT_DB_LOCK, SNAPSHOT_DB_CONTENT_LOCK,
+                SNAPSHOT_LOCAL_DATA_LOCK));
+    List<DAGLeveledResource> resources = 
Arrays.stream(DAGLeveledResource.values()).collect(Collectors.toList());
+    for (DAGLeveledResource otherResource : resources) {
+      String otherResourceName1 = otherResource.getName() + "key";
+      String otherResourceName2 = otherResource.getName() + "key";
+      String flatResourceName = dagLeveledResource.getName() + "key";
+      try (HierarchicalResourceLock lock1 = 
lockManager.acquireWriteLock(otherResource, otherResourceName1);
+           HierarchicalResourceLock lock2 = 
lockManager.acquireWriteLock(otherResource, otherResourceName2)) {

Review Comment:
   is this to ensure a lock is reentrant? because otherResourceName1 and 
otherResourceName2 would be the same.



##########
hadoop-ozone/common/src/test/java/org/apache/hadoop/ozone/om/lock/TestPoolBasedHierarchicalResourceLockManager.java:
##########
@@ -580,4 +572,39 @@ public void testIOExceptionPropagation() {
       assertNotNull(e.getMessage());
     }
   }
+
+  @ParameterizedTest
+  @EnumSource
+  public void testDAGLockOrderAcquisition(DAGLeveledResource 
dagLeveledResource) throws IOException {
+    Map<DAGLeveledResource, Set<IOzoneManagerLock.Resource>> 
forbiddenLockOrdering =
+        ImmutableMap.of(SNAPSHOT_DB_CONTENT_LOCK, 
ImmutableSet.of(SNAPSHOT_DB_LOCK, SNAPSHOT_LOCAL_DATA_LOCK),
+            BOOTSTRAP_LOCK, ImmutableSet.of(SNAPSHOT_GC_LOCK, 
SNAPSHOT_DB_LOCK, SNAPSHOT_DB_CONTENT_LOCK,
+                SNAPSHOT_LOCAL_DATA_LOCK));
+    List<DAGLeveledResource> resources = 
Arrays.stream(DAGLeveledResource.values()).collect(Collectors.toList());
+    for (DAGLeveledResource otherResource : resources) {
+      String otherResourceName1 = otherResource.getName() + "key";
+      String otherResourceName2 = otherResource.getName() + "key";
+      String flatResourceName = dagLeveledResource.getName() + "key";

Review Comment:
   String dagResourceName = dagLeveledResource.getName() + "key";



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