arp7 commented on a change in pull request #949: HDDS-1672. Improve locking in 
OzoneManager.
URL: https://github.com/apache/hadoop/pull/949#discussion_r295930273
 
 

 ##########
 File path: 
hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/om/OzoneManagerLock.java
 ##########
 @@ -154,79 +178,137 @@ public void releaseVolumeLock(String volume) {
   }
 
   /**
-   * Acquires S3 Bucket lock on the given resource.
+   * Acquires bucket lock on the given resource.
    *
    * <p>If the lock is not available then the current thread becomes
-   * disabled for thread scheduling purposes and lies dormant until the lock 
has
-   * been acquired.
+   * disabled for thread scheduling purposes and lies dormant until the
+   * lock has been acquired.
    *
-   * @param s3BucketName S3Bucket Name on which the lock has to be acquired
+   * @param bucket Bucket on which the lock has to be acquired
    */
-  public void acquireS3Lock(String s3BucketName) {
-    // Calling thread should not hold any bucket lock.
-    // You can take an Volume while holding S3 bucket lock, since
-    // semantically an S3 bucket maps to the ozone volume. So we check here
-    // only if ozone bucket lock is taken.
-    if (hasAnyBucketLock()) {
+  public void acquireBucketLock(String volume, String bucket) {
+    if (hasAnyUserLock()) {
       throw new RuntimeException(
           "Thread '" + Thread.currentThread().getName() +
-              "' cannot acquire S3 bucket lock while holding Ozone bucket " +
-              "lock(s).");
+              "' cannot acquire bucket lock while holding User lock.");
     }
-    manager.lock(OM_S3_PREFIX + s3BucketName);
-    myLocks.get().get(S3_BUCKET_LOCK).incrementAndGet();
+    manager.lock(OM_KEY_PREFIX + volume + OM_KEY_PREFIX + bucket);
+    myLocks.get().get(BUCKET_LOCK).incrementAndGet();
   }
 
   /**
-   * Releases the volume lock on given resource.
+   * Releases the bucket lock on given resource.
    */
-  public void releaseS3Lock(String s3BucketName) {
-    manager.unlock(OM_S3_PREFIX + s3BucketName);
-    myLocks.get().get(S3_BUCKET_LOCK).decrementAndGet();
+  public void releaseBucketLock(String volume, String bucket) {
+    manager.unlock(OM_KEY_PREFIX + volume + OM_KEY_PREFIX + bucket);
+    myLocks.get().get(BUCKET_LOCK).decrementAndGet();
   }
 
   /**
-   * Acquires bucket lock on the given resource.
+   * Acquires user lock on the given resource.
    *
    * <p>If the lock is not available then the current thread becomes
    * disabled for thread scheduling purposes and lies dormant until the
    * lock has been acquired.
    *
-   * @param bucket Bucket on which the lock has to be acquired
+   * @param user User on which the lock has to be acquired
    */
-  public void acquireBucketLock(String volume, String bucket) {
-    manager.lock(OM_KEY_PREFIX + volume + OM_KEY_PREFIX + bucket);
-    myLocks.get().get(BUCKET_LOCK).incrementAndGet();
+  public void acquireUserLock(String user) {
+    // In order to not maintain username's on which we have acquired lock,
+    // just checking have we acquired userLock before. If user want's to
+    // acquire user lock on multiple user's they should use
+    // acquireMultiUserLock. This is just a protection logic, to let not users
+    // use this if acquiring lock on multiple users. As currently, we have only
+    // use case we have for this is during setOwner operation in VolumeManager.
+    if (hasAnyUserLock()) {
+      LOG.error("Already have userLock");
+      throw new RuntimeException("For acquiring lock on multiple users, use " +
+          "acquireMultiLock method");
+    }
+    manager.lock(OM_USER_PREFIX + user);
+    myLocks.get().get(USER_LOCK).incrementAndGet();
   }
 
   /**
-   * Releases the bucket lock on given resource.
+   * Releases the user lock on given resource.
    */
-  public void releaseBucketLock(String volume, String bucket) {
-    manager.unlock(OM_KEY_PREFIX + volume + OM_KEY_PREFIX + bucket);
-    myLocks.get().get(BUCKET_LOCK).decrementAndGet();
+  public void releaseUserLock(String user) {
+    manager.unlock(OM_USER_PREFIX + user);
+    myLocks.get().get(USER_LOCK).decrementAndGet();
   }
 
   /**
-   * Returns true if the current thread holds any volume lock.
-   * @return true if current thread holds volume lock, else false
+   * Acquire user lock on 2 users. In this case, we compare 2 strings
+   * lexicographically, and acquire the locks according to the sorted order of
+   * the user names. In this way, when acquiring locks on multiple user's, we
+   * can avoid dead locks. This method should be called when single thread is
+   * acquiring lock on 2 users at a time.
+   *
+   * Example:
+   * ozone, hdfs -> lock acquire order will be hdfs, ozone
+   * hdfs, ozone -> lock acquire order will be hdfs, ozone
+   *
+   * @param newUser
+   * @param oldUser
    */
-  private boolean hasAnyVolumeLock() {
-    return myLocks.get().get(VOLUME_LOCK).get() != 0;
+  public void acquireMultiUserLock(String newUser, String oldUser) {
+    Preconditions.checkNotNull(newUser);
+    Preconditions.checkNotNull(oldUser);
+    int compare = newUser.compareTo(oldUser);
+
+    if (compare < 0) {
+      manager.lock(OM_USER_PREFIX + newUser);
+      manager.lock(OM_USER_PREFIX + oldUser);
 
 Review comment:
   If the second lock call fails then we should release the first lock before 
returning.
   
   The method should either get both locks or none.

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