Skip to content

HDDS-1727. Use generation of resourceName for locks in OzoneManagerLock. #1014

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ public static Versioning getVersioning(boolean versioning) {
public static final String OM_S3_PREFIX ="S3:";
public static final String OM_S3_VOLUME_PREFIX = "s3";
public static final String OM_S3_SECRET = "S3Secret:";
public static final String OM_PREFIX = "Prefix:";

/**
* Max chunk size limit.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,14 @@ public OzoneManagerLock(Configuration conf) {
*
* 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.
* @param resources - Resource names on which user want to acquire lock.
* For Resource type bucket, first param should be volume, second param
* should be bucket name. For remaining all resource only one param should
* be passed.
*/
public void acquireLock(String resourceName, Resource resource) {
public void acquireLock(Resource resource, String... resources) {
String resourceName = generateResourceName(resource, resources);
if (!resource.canLock(lockSet.get())) {
String errorMessage = getErrorMessage(resource);
LOG.error(errorMessage);
Expand All @@ -115,6 +119,24 @@ public void acquireLock(String resourceName, Resource resource) {
}
}

/**
* Generate resource name to be locked.
* @param resource
* @param resources
*/
private String generateResourceName(Resource resource, String... resources) {
if (resources.length == 1 && resource != Resource.BUCKET) {
return OzoneManagerLockUtil.generateResourceLockName(resource,
resources[0]);
} else if (resources.length == 2 && resource == Resource.BUCKET) {
return OzoneManagerLockUtil.generateBucketLockName(resources[0],
resources[1]);
} else {
throw new IllegalArgumentException("acquire lock is supported on single" +
" resource for all locks except for resource bucket");
}
}

private String getErrorMessage(Resource resource) {
return "Thread '" + Thread.currentThread().getName() + "' cannot " +
"acquire " + resource.name + " lock while holding " +
Expand All @@ -124,7 +146,6 @@ private String getErrorMessage(Resource resource) {

private List<String> getCurrentLocks() {
List<String> currentLocks = new ArrayList<>();
int i=0;
short lockSetVal = lockSet.get();
for (Resource value : Resource.values()) {
if (value.isLevelLocked(lockSetVal)) {
Expand All @@ -141,6 +162,9 @@ private List<String> getCurrentLocks() {
*/
public void acquireMultiUserLock(String firstUser, String secondUser) {
Resource resource = Resource.USER;
firstUser = generateResourceName(resource, firstUser);
secondUser = generateResourceName(resource, secondUser);

if (!resource.canLock(lockSet.get())) {
String errorMessage = getErrorMessage(resource);
LOG.error(errorMessage);
Expand Down Expand Up @@ -199,10 +223,12 @@ public void acquireMultiUserLock(String firstUser, String secondUser) {
*/
public void releaseMultiUserLock(String firstUser, String secondUser) {
Resource resource = Resource.USER;
firstUser = generateResourceName(resource, firstUser);
secondUser = generateResourceName(resource, secondUser);

int compare = firstUser.compareTo(secondUser);

String temp;

// Order the user names in sorted order. Swap them.
if (compare > 0) {
temp = secondUser;
Expand All @@ -222,9 +248,16 @@ public void releaseMultiUserLock(String firstUser, String secondUser) {
lockSet.set(resource.clearLock(lockSet.get()));
}


public void releaseLock(String resourceName, Resource resource) {

/**
* Release lock on resource.
* @param resource - Type of the resource.
* @param resources - Resource names on which user want to acquire lock.
* For Resource type bucket, first param should be volume, second param
* should be bucket name. For remaining all resource only one param should
* be passed.
*/
public void releaseLock(Resource resource, String... resources) {
String resourceName = generateResourceName(resource, resources);
// 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@
package org.apache.hadoop.ozone.om.lock;

import static org.apache.hadoop.ozone.OzoneConsts.OM_KEY_PREFIX;
import static org.apache.hadoop.ozone.OzoneConsts.OM_PREFIX;
import static org.apache.hadoop.ozone.OzoneConsts.OM_S3_PREFIX;
import static org.apache.hadoop.ozone.OzoneConsts.OM_S3_SECRET;
import static org.apache.hadoop.ozone.OzoneConsts.OM_USER_PREFIX;

/**
* Utility class contains helper functions required for OM lock.
*/
public final class OzoneManagerLockUtil {
final class OzoneManagerLockUtil {


private OzoneManagerLockUtil() {
Expand All @@ -50,7 +51,7 @@ public static String generateResourceLockName(
} else if (resource == OzoneManagerLock.Resource.S3_SECRET) {
return OM_S3_SECRET + resourceName;
} else if (resource == OzoneManagerLock.Resource.PREFIX) {
return OM_S3_PREFIX + resourceName;
return OM_PREFIX + resourceName;
} else {
// This is for developers who mistakenly call this method with resource
// bucket type, as for bucket type we need bucket and volumeName.
Expand Down
Loading