[GitHub] [hadoop] bharatviswa504 commented on a change in pull request #1006: HDDS-1723. Create new OzoneManagerLock class.

2019-06-24 Thread GitBox
bharatviswa504 commented on a change in pull request #1006: HDDS-1723. Create 
new OzoneManagerLock class.
URL: https://github.com/apache/hadoop/pull/1006#discussion_r297003720
 
 

 ##
 File path: 
hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/om/lock/OzoneManagerLock.java
 ##
 @@ -0,0 +1,336 @@
+/**
+ * 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.
+ *
+ * 
+ *   
+ *   
+ *  WEIGHT   LOCK 
+ *   
+ *   
+ *  0   S3 Bucket Lock 
+ *   
+ *   
+ *  1   Volume Lock 
+ *   
+ *   
+ *  2   Bucket Lock 
+ *   
+ *   
+ *  3   User Lock 
+ *   
+ *   
+ *  4   S3 Secret Lock
+ *   
+ *   
+ *  5   Prefix Lock 
+ *   
+ * 
+ *
+ * One cannot obtain a lower weight lock while holding a lock with higher
+ * weight. The other way around is possible. 
+ * 
+ * 
+ * For example:
+ * 
+ * {@literal ->} acquire volume lock (will work)
+ *   {@literal +->} acquire bucket lock (will work)
+ * {@literal +-->} acquire s3 bucket lock (will throw Exception)
+ * 
+ * 
+ */
+
+public class OzoneManagerLock {
+
+  private static final Logger LOG =
+  LoggerFactory.getLogger(OzoneManagerLock.class);
+
+  private final LockManager manager;
+  private final ThreadLocal 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 getCurrentLocks() {
+List currentLocks = new ArrayList<>();
+int i=0;
+short lockSetVal = lockSet.get();
+for (Resource value : Resource.values()) {
+  if (value.isLevelLocked(lockSetVal)) {
+currentLocks.add(value.getName());
+  }
+}
+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 {
+  // When acquiring multiple user locks, the reason for doing lexical
+  // order comparision is to avoid deadlock scenario.
+
+  // Example: 1st thread acquire lock(ozone, hdfs)
+  // 2nd thread acquire lock(hdfs, ozone).
+  // If we don't acquire user locks in an order, there can be a deadlock.
+
+  // 1st thread acquired lock on ozone, waiting for lock on hdfs, 2nd
+  // thread acquired lock on 

[GitHub] [hadoop] bharatviswa504 commented on a change in pull request #1006: HDDS-1723. Create new OzoneManagerLock class.

2019-06-24 Thread GitBox
bharatviswa504 commented on a change in pull request #1006: HDDS-1723. Create 
new OzoneManagerLock class.
URL: https://github.com/apache/hadoop/pull/1006#discussion_r297003664
 
 

 ##
 File path: 
hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/om/lock/OzoneManagerLock.java
 ##
 @@ -0,0 +1,336 @@
+/**
+ * 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.
+ *
+ * 
+ *   
+ *   
+ *  WEIGHT   LOCK 
+ *   
+ *   
+ *  0   S3 Bucket Lock 
+ *   
+ *   
+ *  1   Volume Lock 
+ *   
+ *   
+ *  2   Bucket Lock 
+ *   
+ *   
+ *  3   User Lock 
+ *   
+ *   
+ *  4   S3 Secret Lock
+ *   
+ *   
+ *  5   Prefix Lock 
+ *   
+ * 
+ *
+ * One cannot obtain a lower weight lock while holding a lock with higher
+ * weight. The other way around is possible. 
+ * 
+ * 
+ * For example:
+ * 
+ * {@literal ->} acquire volume lock (will work)
+ *   {@literal +->} acquire bucket lock (will work)
+ * {@literal +-->} acquire s3 bucket lock (will throw Exception)
+ * 
+ * 
+ */
+
+public class OzoneManagerLock {
+
+  private static final Logger LOG =
+  LoggerFactory.getLogger(OzoneManagerLock.class);
+
+  private final LockManager manager;
+  private final ThreadLocal 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 getCurrentLocks() {
+List currentLocks = new ArrayList<>();
+int i=0;
+short lockSetVal = lockSet.get();
+for (Resource value : Resource.values()) {
+  if (value.isLevelLocked(lockSetVal)) {
+currentLocks.add(value.getName());
+  }
+}
+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 {
+  // When acquiring multiple user locks, the reason for doing lexical
+  // order comparision is to avoid deadlock scenario.
+
+  // Example: 1st thread acquire lock(ozone, hdfs)
+  // 2nd thread acquire lock(hdfs, ozone).
+  // If we don't acquire user locks in an order, there can be a deadlock.
+
+  // 1st thread acquired lock on ozone, waiting for lock on hdfs, 2nd
+  // thread acquired lock on 

[GitHub] [hadoop] bharatviswa504 commented on a change in pull request #1006: HDDS-1723. Create new OzoneManagerLock class.

2019-06-24 Thread GitBox
bharatviswa504 commented on a change in pull request #1006: HDDS-1723. Create 
new OzoneManagerLock class.
URL: https://github.com/apache/hadoop/pull/1006#discussion_r297000862
 
 

 ##
 File path: 
hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/om/lock/OzoneManagerLock.java
 ##
 @@ -0,0 +1,336 @@
+/**
+ * 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.
+ *
+ * 
+ *   
+ *   
+ *  WEIGHT   LOCK 
+ *   
+ *   
+ *  0   S3 Bucket Lock 
+ *   
+ *   
+ *  1   Volume Lock 
+ *   
+ *   
+ *  2   Bucket Lock 
+ *   
+ *   
+ *  3   User Lock 
+ *   
+ *   
+ *  4   S3 Secret Lock
+ *   
+ *   
+ *  5   Prefix Lock 
+ *   
+ * 
+ *
+ * One cannot obtain a lower weight lock while holding a lock with higher
+ * weight. The other way around is possible. 
+ * 
+ * 
+ * For example:
+ * 
+ * {@literal ->} acquire volume lock (will work)
+ *   {@literal +->} acquire bucket lock (will work)
+ * {@literal +-->} acquire s3 bucket lock (will throw Exception)
+ * 
+ * 
+ */
+
+public class OzoneManagerLock {
+
+  private static final Logger LOG =
+  LoggerFactory.getLogger(OzoneManagerLock.class);
+
+  private final LockManager manager;
+  private final ThreadLocal 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 getCurrentLocks() {
+List currentLocks = new ArrayList<>();
+int i=0;
+short lockSetVal = lockSet.get();
+for (Resource value : Resource.values()) {
+  if (value.isLevelLocked(lockSetVal)) {
+currentLocks.add(value.getName());
+  }
+}
+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 {
+  // When acquiring multiple user locks, the reason for doing lexical
+  // order comparision is to avoid deadlock scenario.
+
+  // Example: 1st thread acquire lock(ozone, hdfs)
+  // 2nd thread acquire lock(hdfs, ozone).
+  // If we don't acquire user locks in an order, there can be a deadlock.
+
+  // 1st thread acquired lock on ozone, waiting for lock on hdfs, 2nd
+  // thread acquired lock on 

[GitHub] [hadoop] bharatviswa504 commented on a change in pull request #1006: HDDS-1723. Create new OzoneManagerLock class.

2019-06-24 Thread GitBox
bharatviswa504 commented on a change in pull request #1006: HDDS-1723. Create 
new OzoneManagerLock class.
URL: https://github.com/apache/hadoop/pull/1006#discussion_r297000862
 
 

 ##
 File path: 
hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/om/lock/OzoneManagerLock.java
 ##
 @@ -0,0 +1,336 @@
+/**
+ * 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.
+ *
+ * 
+ *   
+ *   
+ *  WEIGHT   LOCK 
+ *   
+ *   
+ *  0   S3 Bucket Lock 
+ *   
+ *   
+ *  1   Volume Lock 
+ *   
+ *   
+ *  2   Bucket Lock 
+ *   
+ *   
+ *  3   User Lock 
+ *   
+ *   
+ *  4   S3 Secret Lock
+ *   
+ *   
+ *  5   Prefix Lock 
+ *   
+ * 
+ *
+ * One cannot obtain a lower weight lock while holding a lock with higher
+ * weight. The other way around is possible. 
+ * 
+ * 
+ * For example:
+ * 
+ * {@literal ->} acquire volume lock (will work)
+ *   {@literal +->} acquire bucket lock (will work)
+ * {@literal +-->} acquire s3 bucket lock (will throw Exception)
+ * 
+ * 
+ */
+
+public class OzoneManagerLock {
+
+  private static final Logger LOG =
+  LoggerFactory.getLogger(OzoneManagerLock.class);
+
+  private final LockManager manager;
+  private final ThreadLocal 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 getCurrentLocks() {
+List currentLocks = new ArrayList<>();
+int i=0;
+short lockSetVal = lockSet.get();
+for (Resource value : Resource.values()) {
+  if (value.isLevelLocked(lockSetVal)) {
+currentLocks.add(value.getName());
+  }
+}
+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 {
+  // When acquiring multiple user locks, the reason for doing lexical
+  // order comparision is to avoid deadlock scenario.
+
+  // Example: 1st thread acquire lock(ozone, hdfs)
+  // 2nd thread acquire lock(hdfs, ozone).
+  // If we don't acquire user locks in an order, there can be a deadlock.
+
+  // 1st thread acquired lock on ozone, waiting for lock on hdfs, 2nd
+  // thread acquired lock on 

[GitHub] [hadoop] bharatviswa504 commented on a change in pull request #1006: HDDS-1723. Create new OzoneManagerLock class.

2019-06-24 Thread GitBox
bharatviswa504 commented on a change in pull request #1006: HDDS-1723. Create 
new OzoneManagerLock class.
URL: https://github.com/apache/hadoop/pull/1006#discussion_r297000629
 
 

 ##
 File path: 
hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/om/lock/OzoneManagerLock.java
 ##
 @@ -0,0 +1,336 @@
+/**
+ * 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.
+ *
+ * 
+ *   
+ *   
+ *  WEIGHT   LOCK 
+ *   
+ *   
+ *  0   S3 Bucket Lock 
+ *   
+ *   
+ *  1   Volume Lock 
+ *   
+ *   
+ *  2   Bucket Lock 
+ *   
+ *   
+ *  3   User Lock 
+ *   
+ *   
+ *  4   S3 Secret Lock
+ *   
+ *   
+ *  5   Prefix Lock 
+ *   
+ * 
+ *
+ * One cannot obtain a lower weight lock while holding a lock with higher
+ * weight. The other way around is possible. 
+ * 
+ * 
+ * For example:
+ * 
+ * {@literal ->} acquire volume lock (will work)
+ *   {@literal +->} acquire bucket lock (will work)
+ * {@literal +-->} acquire s3 bucket lock (will throw Exception)
+ * 
+ * 
+ */
+
+public class OzoneManagerLock {
+
+  private static final Logger LOG =
+  LoggerFactory.getLogger(OzoneManagerLock.class);
+
+  private final LockManager manager;
+  private final ThreadLocal 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 getCurrentLocks() {
+List currentLocks = new ArrayList<>();
+int i=0;
+short lockSetVal = lockSet.get();
+for (Resource value : Resource.values()) {
+  if (value.isLevelLocked(lockSetVal)) {
+currentLocks.add(value.getName());
+  }
+}
+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 {
+  // When acquiring multiple user locks, the reason for doing lexical
+  // order comparision is to avoid deadlock scenario.
+
+  // Example: 1st thread acquire lock(ozone, hdfs)
+  // 2nd thread acquire lock(hdfs, ozone).
+  // If we don't acquire user locks in an order, there can be a deadlock.
+
+  // 1st thread acquired lock on ozone, waiting for lock on hdfs, 2nd
+  // thread acquired lock on 

[GitHub] [hadoop] bharatviswa504 commented on a change in pull request #1006: HDDS-1723. Create new OzoneManagerLock class.

2019-06-24 Thread GitBox
bharatviswa504 commented on a change in pull request #1006: HDDS-1723. Create 
new OzoneManagerLock class.
URL: https://github.com/apache/hadoop/pull/1006#discussion_r297000650
 
 

 ##
 File path: 
hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/om/lock/OzoneManagerLock.java
 ##
 @@ -0,0 +1,336 @@
+/**
+ * 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.
+ *
+ * 
+ *   
+ *   
+ *  WEIGHT   LOCK 
+ *   
+ *   
+ *  0   S3 Bucket Lock 
+ *   
+ *   
+ *  1   Volume Lock 
+ *   
+ *   
+ *  2   Bucket Lock 
+ *   
+ *   
+ *  3   User Lock 
+ *   
+ *   
+ *  4   S3 Secret Lock
+ *   
+ *   
+ *  5   Prefix Lock 
+ *   
+ * 
+ *
+ * One cannot obtain a lower weight lock while holding a lock with higher
+ * weight. The other way around is possible. 
+ * 
+ * 
+ * For example:
+ * 
+ * {@literal ->} acquire volume lock (will work)
+ *   {@literal +->} acquire bucket lock (will work)
+ * {@literal +-->} acquire s3 bucket lock (will throw Exception)
+ * 
+ * 
+ */
+
+public class OzoneManagerLock {
+
+  private static final Logger LOG =
+  LoggerFactory.getLogger(OzoneManagerLock.class);
+
+  private final LockManager manager;
+  private final ThreadLocal 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 getCurrentLocks() {
+List currentLocks = new ArrayList<>();
+int i=0;
+short lockSetVal = lockSet.get();
+for (Resource value : Resource.values()) {
+  if (value.isLevelLocked(lockSetVal)) {
+currentLocks.add(value.getName());
+  }
+}
+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 {
+  // When acquiring multiple user locks, the reason for doing lexical
+  // order comparision is to avoid deadlock scenario.
+
+  // Example: 1st thread acquire lock(ozone, hdfs)
+  // 2nd thread acquire lock(hdfs, ozone).
+  // If we don't acquire user locks in an order, there can be a deadlock.
+
+  // 1st thread acquired lock on ozone, waiting for lock on hdfs, 2nd
+  // thread acquired lock on 

[GitHub] [hadoop] bharatviswa504 commented on a change in pull request #1006: HDDS-1723. Create new OzoneManagerLock class.

2019-06-24 Thread GitBox
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.
+ *
+ * 
+ *   
+ *   
+ *  WEIGHT   LOCK 
+ *   
+ *   
+ *  0   S3 Bucket Lock 
+ *   
+ *   
+ *  1   Volume Lock 
+ *   
+ *   
+ *  2   Bucket Lock 
+ *   
+ *   
+ *  3   User Lock 
+ *   
+ *   
+ *  4   S3 Secret Lock
+ *   
+ *   
+ *  5   Prefix Lock 
+ *   
+ * 
+ *
+ * One cannot obtain a lower weight lock while holding a lock with higher
+ * weight. The other way around is possible. 
+ * 
+ * 
+ * For example:
+ * 
+ * {@literal ->} acquire volume lock (will work)
+ *   {@literal +->} acquire bucket lock (will work)
+ * {@literal +-->} acquire s3 bucket lock (will throw Exception)
+ * 
+ * 
+ */
+
+public class OzoneManagerLock {
+
+  private static final Logger LOG =
+  LoggerFactory.getLogger(OzoneManagerLock.class);
+
+  private final LockManager manager;
+  private final ThreadLocal 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 getCurrentLocks() {
+List 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);
+  

[GitHub] [hadoop] bharatviswa504 commented on a change in pull request #1006: HDDS-1723. Create new OzoneManagerLock class.

2019-06-24 Thread GitBox
bharatviswa504 commented on a change in pull request #1006: HDDS-1723. Create 
new OzoneManagerLock class.
URL: https://github.com/apache/hadoop/pull/1006#discussion_r296931497
 
 

 ##
 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.
+ *
+ * 
+ *   
+ *   
+ *  WEIGHT   LOCK 
+ *   
+ *   
+ *  0   S3 Bucket Lock 
+ *   
+ *   
+ *  1   Volume Lock 
+ *   
+ *   
+ *  2   Bucket Lock 
+ *   
+ *   
+ *  3   User Lock 
+ *   
+ *   
+ *  4   S3 Secret Lock
+ *   
+ *   
+ *  5   Prefix Lock 
+ *   
+ * 
+ *
+ * One cannot obtain a lower weight lock while holding a lock with higher
+ * weight. The other way around is possible. 
+ * 
+ * 
+ * For example:
+ * 
+ * {@literal ->} acquire volume lock (will work)
+ *   {@literal +->} acquire bucket lock (will work)
+ * {@literal +-->} acquire s3 bucket lock (will throw Exception)
+ * 
+ * 
+ */
+
+public class OzoneManagerLock {
+
+  private static final Logger LOG =
+  LoggerFactory.getLogger(OzoneManagerLock.class);
+
+  private final LockManager manager;
+  private final ThreadLocal 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 getCurrentLocks() {
+List 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);
 
 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 

[GitHub] [hadoop] bharatviswa504 commented on a change in pull request #1006: HDDS-1723. Create new OzoneManagerLock class.

2019-06-24 Thread GitBox
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.
+ *
+ * 
+ *   
+ *   
+ *  WEIGHT   LOCK 
+ *   
+ *   
+ *  0   S3 Bucket Lock 
+ *   
+ *   
+ *  1   Volume Lock 
+ *   
+ *   
+ *  2   Bucket Lock 
+ *   
+ *   
+ *  3   User Lock 
+ *   
+ *   
+ *  4   S3 Secret Lock
+ *   
+ *   
+ *  5   Prefix Lock 
+ *   
+ * 
+ *
+ * One cannot obtain a lower weight lock while holding a lock with higher
+ * weight. The other way around is possible. 
+ * 
+ * 
+ * For example:
+ * 
+ * {@literal ->} acquire volume lock (will work)
+ *   {@literal +->} acquire bucket lock (will work)
+ * {@literal +-->} acquire s3 bucket lock (will throw Exception)
+ * 
+ * 
+ */
+
+public class OzoneManagerLock {
+
+  private static final Logger LOG =
+  LoggerFactory.getLogger(OzoneManagerLock.class);
+
+  private final LockManager manager;
+  private final ThreadLocal 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 getCurrentLocks() {
+List 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);
+  

[GitHub] [hadoop] bharatviswa504 commented on a change in pull request #1006: HDDS-1723. Create new OzoneManagerLock class.

2019-06-24 Thread GitBox
bharatviswa504 commented on a change in pull request #1006: HDDS-1723. Create 
new OzoneManagerLock class.
URL: https://github.com/apache/hadoop/pull/1006#discussion_r296925887
 
 

 ##
 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.
+ *
+ * 
+ *   
+ *   
+ *  WEIGHT   LOCK 
+ *   
+ *   
+ *  0   S3 Bucket Lock 
+ *   
+ *   
+ *  1   Volume Lock 
+ *   
+ *   
+ *  2   Bucket Lock 
+ *   
+ *   
+ *  3   User Lock 
+ *   
+ *   
+ *  4   S3 Secret Lock
+ *   
+ *   
+ *  5   Prefix Lock 
+ *   
+ * 
+ *
+ * One cannot obtain a lower weight lock while holding a lock with higher
+ * weight. The other way around is possible. 
+ * 
+ * 
+ * For example:
+ * 
+ * {@literal ->} acquire volume lock (will work)
+ *   {@literal +->} acquire bucket lock (will work)
+ * {@literal +-->} acquire s3 bucket lock (will throw Exception)
+ * 
+ * 
+ */
+
+public class OzoneManagerLock {
+
+  private static final Logger LOG =
+  LoggerFactory.getLogger(OzoneManagerLock.class);
+
+  private final LockManager manager;
+  private final ThreadLocal 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 getCurrentLocks() {
+List 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
+   */
 
 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



[GitHub] [hadoop] bharatviswa504 commented on a change in pull request #1006: HDDS-1723. Create new OzoneManagerLock class.

2019-06-24 Thread GitBox
bharatviswa504 commented on a change in pull request #1006: HDDS-1723. Create 
new OzoneManagerLock class.
URL: https://github.com/apache/hadoop/pull/1006#discussion_r296923309
 
 

 ##
 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.
+ *
+ * 
+ *   
+ *   
+ *  WEIGHT   LOCK 
+ *   
+ *   
+ *  0   S3 Bucket Lock 
+ *   
+ *   
+ *  1   Volume Lock 
+ *   
+ *   
+ *  2   Bucket Lock 
+ *   
+ *   
+ *  3   User Lock 
+ *   
+ *   
+ *  4   S3 Secret Lock
+ *   
+ *   
+ *  5   Prefix Lock 
+ *   
+ * 
+ *
+ * One cannot obtain a lower weight lock while holding a lock with higher
+ * weight. The other way around is possible. 
+ * 
+ * 
+ * For example:
+ * 
+ * {@literal ->} acquire volume lock (will work)
+ *   {@literal +->} acquire bucket lock (will work)
+ * {@literal +-->} acquire s3 bucket lock (will throw Exception)
+ * 
+ * 
+ */
+
+public class OzoneManagerLock {
+
+  private static final Logger LOG =
+  LoggerFactory.getLogger(OzoneManagerLock.class);
+
+  private final LockManager manager;
+  private final ThreadLocal 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 getCurrentLocks() {
+List 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;
 
 Review comment:
   Yes, when we acquire lock from the OzoneManager methods like createVolume 
etc., we do those checks and then call acquire the lock. For now, I will avoid 
the checks.


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 

[GitHub] [hadoop] bharatviswa504 commented on a change in pull request #1006: HDDS-1723. Create new OzoneManagerLock class.

2019-06-24 Thread GitBox
bharatviswa504 commented on a change in pull request #1006: HDDS-1723. Create 
new OzoneManagerLock class.
URL: https://github.com/apache/hadoop/pull/1006#discussion_r296923309
 
 

 ##
 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.
+ *
+ * 
+ *   
+ *   
+ *  WEIGHT   LOCK 
+ *   
+ *   
+ *  0   S3 Bucket Lock 
+ *   
+ *   
+ *  1   Volume Lock 
+ *   
+ *   
+ *  2   Bucket Lock 
+ *   
+ *   
+ *  3   User Lock 
+ *   
+ *   
+ *  4   S3 Secret Lock
+ *   
+ *   
+ *  5   Prefix Lock 
+ *   
+ * 
+ *
+ * One cannot obtain a lower weight lock while holding a lock with higher
+ * weight. The other way around is possible. 
+ * 
+ * 
+ * For example:
+ * 
+ * {@literal ->} acquire volume lock (will work)
+ *   {@literal +->} acquire bucket lock (will work)
+ * {@literal +-->} acquire s3 bucket lock (will throw Exception)
+ * 
+ * 
+ */
+
+public class OzoneManagerLock {
+
+  private static final Logger LOG =
+  LoggerFactory.getLogger(OzoneManagerLock.class);
+
+  private final LockManager manager;
+  private final ThreadLocal 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 getCurrentLocks() {
+List 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;
 
 Review comment:
   Yes, when we acquire lock from the OzoneManager methods like createVolume 
etc.,, we do those checks and then call acquire the lock. For now, I will avoid 
them.


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 

[GitHub] [hadoop] bharatviswa504 commented on a change in pull request #1006: HDDS-1723. Create new OzoneManagerLock class.

2019-06-24 Thread GitBox
bharatviswa504 commented on a change in pull request #1006: HDDS-1723. Create 
new OzoneManagerLock class.
URL: https://github.com/apache/hadoop/pull/1006#discussion_r296921839
 
 

 ##
 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.
+ *
+ * 
+ *   
+ *   
+ *  WEIGHT   LOCK 
+ *   
+ *   
+ *  0   S3 Bucket Lock 
+ *   
+ *   
+ *  1   Volume Lock 
+ *   
+ *   
+ *  2   Bucket Lock 
+ *   
+ *   
+ *  3   User Lock 
+ *   
+ *   
+ *  4   S3 Secret Lock
+ *   
+ *   
+ *  5   Prefix Lock 
+ *   
+ * 
+ *
+ * One cannot obtain a lower weight lock while holding a lock with higher
+ * weight. The other way around is possible. 
+ * 
+ * 
+ * For example:
+ * 
+ * {@literal ->} acquire volume lock (will work)
+ *   {@literal +->} acquire bucket lock (will work)
+ * {@literal +-->} acquire s3 bucket lock (will throw Exception)
+ * 
+ * 
+ */
+
+public class OzoneManagerLock {
+
+  private static final Logger LOG =
+  LoggerFactory.getLogger(OzoneManagerLock.class);
+
+  private final LockManager manager;
+  private final ThreadLocal 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 getCurrentLocks() {
+List currentLocks = new ArrayList<>();
+int i=0;
+short lockSetVal = lockSet.get();
+for (Resource value : Resource.values()) {
+  if ((lockSetVal & value.setMask) == value.setMask) {
 
 Review comment:
   I have added an isLevelLocked function and still left the getCurrentLocks() 
in the OzoneManagerLock itself.


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