bharatviswa504 commented on a change in pull request #1006: HDDS-1723. Create 
new OzoneManagerLock class.
URL: https://github.com/apache/hadoop/pull/1006#discussion_r296926683
 
 

 ##########
 File path: 
hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/om/lock/OzoneManagerLock.java
 ##########
 @@ -0,0 +1,312 @@
+/**
+ * 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.ArrayList;
+import java.util.List;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.ozone.lock.LockManager;
+
+/**
+ * Provides different locks to handle concurrency in OzoneMaster.
+ * We also maintain lock hierarchy, based on the weight.
+ *
+ * <table>
+ *   <caption></caption>
+ *   <tr>
+ *     <td><b> WEIGHT </b></td> <td><b> LOCK </b></td>
+ *   </tr>
+ *   <tr>
+ *     <td> 0 </td> <td> S3 Bucket Lock </td>
+ *   </tr>
+ *   <tr>
+ *     <td> 1 </td> <td> Volume Lock </td>
+ *   </tr>
+ *   <tr>
+ *     <td> 2 </td> <td> Bucket Lock </td>
+ *   </tr>
+ *   <tr>
+ *     <td> 3 </td> <td> User Lock </td>
+ *   </tr>
+ *   <tr>
+ *     <td> 4 </td> <td> S3 Secret Lock</td>
+ *   </tr>
+ *   <tr>
+ *     <td> 5 </td> <td> Prefix Lock </td>
+ *   </tr>
+ * </table>
+ *
+ * One cannot obtain a lower weight lock while holding a lock with higher
+ * weight. The other way around is possible. <br>
+ * <br>
+ * <p>
+ * For example:
+ * <br>
+ * {@literal ->} acquire volume lock (will work)<br>
+ *   {@literal +->} acquire bucket lock (will work)<br>
+ *     {@literal +-->} acquire s3 bucket lock (will throw Exception)<br>
+ * </p>
+ * <br>
+ */
+
+public class OzoneManagerLock {
+
+  private static final Logger LOG =
+      LoggerFactory.getLogger(OzoneManagerLock.class);
+
+  private final LockManager<String> manager;
+  private final ThreadLocal<Short> lockSet = ThreadLocal.withInitial(
+      () -> Short.valueOf((short)0));
+
+
+  /**
+   * Creates new OzoneManagerLock instance.
+   * @param conf Configuration object
+   */
+  public OzoneManagerLock(Configuration conf) {
+    manager = new LockManager<>(conf);
+  }
+
+  /**
+   * Acquire lock on resource.
+   *
+   * For S3_Bucket, VOLUME, BUCKET type resource, same thread acquiring lock
+   * again is allowed.
+   *
+   * For USER, PREFIX, S3_SECRET type resource, same thread acquiring lock
+   * again is not allowed.
+   *
+   * Special Note for UserLock: Single thread can acquire single user lock/
+   * multi user lock. But not both at the same time.
+   * @param resourceName - Resource name on which user want to acquire lock.
+   * @param resource - Type of the resource.
+   */
+  public void acquireLock(String resourceName, Resource resource) {
+    if (!resource.canLock(lockSet.get())) {
+      String errorMessage = getErrorMessage(resource);
+      LOG.error(errorMessage);
+      throw new RuntimeException(errorMessage);
+    } else {
+      manager.lock(resourceName);
+      lockSet.set(resource.setLock(lockSet.get()));
+    }
+  }
+
+  private String getErrorMessage(Resource resource) {
+    return "Thread '" + Thread.currentThread().getName() + "' cannot " +
+        "acquire " + resource.name + " lock while holding " +
+        getCurrentLocks().toString() + " lock(s).";
+
+  }
+
+  private List<String> getCurrentLocks() {
+    List<String> currentLocks = new ArrayList<>();
+    int i=0;
+    short lockSetVal = lockSet.get();
+    for (Resource value : Resource.values()) {
+      if ((lockSetVal & value.setMask) == value.setMask) {
+        currentLocks.add(value.name);
+      }
+    }
+    return currentLocks;
+  }
+
+  /**
+   * Acquire lock on multiple users.
+   * @param oldUserResource
+   * @param newUserResource
+   */
+  public void acquireMultiUserLock(String oldUserResource,
+      String newUserResource) {
+    Resource resource = Resource.USER;
+    if (!resource.canLock(lockSet.get())) {
+      String errorMessage = getErrorMessage(resource);
+      LOG.error(errorMessage);
+      throw new RuntimeException(errorMessage);
+    } else {
+      int compare = newUserResource.compareTo(oldUserResource);
+      if (compare < 0) {
+        manager.lock(newUserResource);
+        try {
+          manager.lock(oldUserResource);
+        } catch (Exception ex) {
+          // We got an exception acquiring 2nd user lock. Release already
+          // acquired user lock, and throw exception to the user.
+          manager.unlock(oldUserResource);
+          throw ex;
+        }
+      } else if (compare > 0) {
+        // If this locking fails, we throw exception to user.
+        manager.lock(oldUserResource);
+        try {
+          manager.lock(newUserResource);
+        } catch (Exception ex) {
+          // We got an exception acquiring 2nd user lock. Release already
+          // acquired user lock, and throw exception to the user.
+          manager.unlock(oldUserResource);
+          throw ex;
+        }
+      } else {
+        // both users are equal.
+        manager.lock(oldUserResource);
+      }
+      lockSet.set(resource.setLock(lockSet.get()));
+    }
+  }
+
+  /**
+   * Acquire lock on multiple users.
+   * @param oldUserResource
+   * @param newUserResource
+   */
+  public void releaseMultiUserLock(String oldUserResource,
+      String newUserResource) {
+    Resource resource = Resource.USER;
+    int compare = newUserResource.compareTo(oldUserResource);
+    if (compare < 0) {
+      manager.unlock(newUserResource);
+      manager.unlock(oldUserResource);
+    } else if (compare > 0) {
+      manager.unlock(oldUserResource);
+      manager.unlock(newUserResource);
+    } else {
+      // both users are equal.
+      manager.unlock(oldUserResource);
+    }
+    lockSet.set(resource.clearLock(lockSet.get()));
+  }
+
+
+  public void releaseLock(String resourceName, Resource resource) {
+
+    // TODO: Not checking release of higher order level lock happened while
+    // releasing lower order level lock, as for that we need counter for
+    // locks, as some locks support acquiring lock again.
+    manager.unlock(resourceName);
+    // clear lock
+    lockSet.set(resource.clearLock(lockSet.get()));
+
+  }
+
+  /**
+   * Resource defined in Ozone.
+   */
+  public enum Resource {
+    // For S3 Bucket need to allow only for S3, that should be means only 1.
+    S3_BUCKET((byte) 0, "S3_BUCKET"), // = 1
+
+    // For volume need to allow both s3 bucket and volume. 01 + 10 = 11 (3)
+    VOLUME((byte) 1, "VOLUME"), // = 2
+
+    // For bucket we need to allow both s3 bucket, volume and bucket. Which
+    // is equal to 100 + 010 + 001 = 111 = 4 + 2 + 1 = 7
+    BUCKET((byte) 2, "BUCKET"), // = 4
+
+    // For user we need to allow s3 bucket, volume, bucket and user lock.
+    // Which is 8  4 + 2 + 1 = 15
+    USER((byte) 3, "USER"), // 15
+
+    S3_SECRET((byte) 4, "S3_SECRET"), // 31
+    PREFIX((byte) 5, "PREFIX"); //63
+
+    // level of the resource
+    private byte lockLevel;
+
+    // This will tell the value, till which we can allow locking.
+    private short mask;
+
+    // This value will help during setLock, and also will tell whether we can
+    // re-acquire lock or not.
+    private short setMask;
+
+    // Name of the resource.
+    private String name;
+
+    Resource(byte pos, String name) {
+      this.lockLevel = pos;
+      for (int x = 0; x < lockLevel + 1; x++) {
+        this.mask += (short) Math.pow(2, x);
 
 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.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: common-issues-unsubscr...@hadoop.apache.org
For additional commands, e-mail: common-issues-h...@hadoop.apache.org

Reply via email to