Skip to content

Commit 909e538

Browse files
bharatviswa504ahussein
authored andcommitted
HDDS-1727. Use generation of resourceName for locks in OzoneManagerLock. (apache#1014)
1 parent 0b0672d commit 909e538

File tree

4 files changed

+109
-103
lines changed

4 files changed

+109
-103
lines changed

hadoop-hdds/common/src/main/java/org/apache/hadoop/ozone/OzoneConsts.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ public static Versioning getVersioning(boolean versioning) {
171171
public static final String OM_S3_PREFIX ="S3:";
172172
public static final String OM_S3_VOLUME_PREFIX = "s3";
173173
public static final String OM_S3_SECRET = "S3Secret:";
174+
public static final String OM_PREFIX = "Prefix:";
174175

175176
/**
176177
* Max chunk size limit.

hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/om/lock/OzoneManagerLock.java

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,14 @@ public OzoneManagerLock(Configuration conf) {
9999
*
100100
* Special Note for UserLock: Single thread can acquire single user lock/
101101
* multi user lock. But not both at the same time.
102-
* @param resourceName - Resource name on which user want to acquire lock.
103102
* @param resource - Type of the resource.
103+
* @param resources - Resource names on which user want to acquire lock.
104+
* For Resource type bucket, first param should be volume, second param
105+
* should be bucket name. For remaining all resource only one param should
106+
* be passed.
104107
*/
105-
public void acquireLock(String resourceName, Resource resource) {
108+
public void acquireLock(Resource resource, String... resources) {
109+
String resourceName = generateResourceName(resource, resources);
106110
if (!resource.canLock(lockSet.get())) {
107111
String errorMessage = getErrorMessage(resource);
108112
LOG.error(errorMessage);
@@ -115,6 +119,24 @@ public void acquireLock(String resourceName, Resource resource) {
115119
}
116120
}
117121

122+
/**
123+
* Generate resource name to be locked.
124+
* @param resource
125+
* @param resources
126+
*/
127+
private String generateResourceName(Resource resource, String... resources) {
128+
if (resources.length == 1 && resource != Resource.BUCKET) {
129+
return OzoneManagerLockUtil.generateResourceLockName(resource,
130+
resources[0]);
131+
} else if (resources.length == 2 && resource == Resource.BUCKET) {
132+
return OzoneManagerLockUtil.generateBucketLockName(resources[0],
133+
resources[1]);
134+
} else {
135+
throw new IllegalArgumentException("acquire lock is supported on single" +
136+
" resource for all locks except for resource bucket");
137+
}
138+
}
139+
118140
private String getErrorMessage(Resource resource) {
119141
return "Thread '" + Thread.currentThread().getName() + "' cannot " +
120142
"acquire " + resource.name + " lock while holding " +
@@ -124,7 +146,6 @@ private String getErrorMessage(Resource resource) {
124146

125147
private List<String> getCurrentLocks() {
126148
List<String> currentLocks = new ArrayList<>();
127-
int i=0;
128149
short lockSetVal = lockSet.get();
129150
for (Resource value : Resource.values()) {
130151
if (value.isLevelLocked(lockSetVal)) {
@@ -141,6 +162,9 @@ private List<String> getCurrentLocks() {
141162
*/
142163
public void acquireMultiUserLock(String firstUser, String secondUser) {
143164
Resource resource = Resource.USER;
165+
firstUser = generateResourceName(resource, firstUser);
166+
secondUser = generateResourceName(resource, secondUser);
167+
144168
if (!resource.canLock(lockSet.get())) {
145169
String errorMessage = getErrorMessage(resource);
146170
LOG.error(errorMessage);
@@ -199,10 +223,12 @@ public void acquireMultiUserLock(String firstUser, String secondUser) {
199223
*/
200224
public void releaseMultiUserLock(String firstUser, String secondUser) {
201225
Resource resource = Resource.USER;
226+
firstUser = generateResourceName(resource, firstUser);
227+
secondUser = generateResourceName(resource, secondUser);
228+
202229
int compare = firstUser.compareTo(secondUser);
203230

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

225-
226-
public void releaseLock(String resourceName, Resource resource) {
227-
251+
/**
252+
* Release lock on resource.
253+
* @param resource - Type of the resource.
254+
* @param resources - Resource names on which user want to acquire lock.
255+
* For Resource type bucket, first param should be volume, second param
256+
* should be bucket name. For remaining all resource only one param should
257+
* be passed.
258+
*/
259+
public void releaseLock(Resource resource, String... resources) {
260+
String resourceName = generateResourceName(resource, resources);
228261
// TODO: Not checking release of higher order level lock happened while
229262
// releasing lower order level lock, as for that we need counter for
230263
// locks, as some locks support acquiring lock again.

hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/om/lock/OzoneManagerLockUtil.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@
1919
package org.apache.hadoop.ozone.om.lock;
2020

2121
import static org.apache.hadoop.ozone.OzoneConsts.OM_KEY_PREFIX;
22+
import static org.apache.hadoop.ozone.OzoneConsts.OM_PREFIX;
2223
import static org.apache.hadoop.ozone.OzoneConsts.OM_S3_PREFIX;
2324
import static org.apache.hadoop.ozone.OzoneConsts.OM_S3_SECRET;
2425
import static org.apache.hadoop.ozone.OzoneConsts.OM_USER_PREFIX;
2526

2627
/**
2728
* Utility class contains helper functions required for OM lock.
2829
*/
29-
public final class OzoneManagerLockUtil {
30+
final class OzoneManagerLockUtil {
3031

3132

3233
private OzoneManagerLockUtil() {
@@ -50,7 +51,7 @@ public static String generateResourceLockName(
5051
} else if (resource == OzoneManagerLock.Resource.S3_SECRET) {
5152
return OM_S3_SECRET + resourceName;
5253
} else if (resource == OzoneManagerLock.Resource.PREFIX) {
53-
return OM_S3_PREFIX + resourceName;
54+
return OM_PREFIX + resourceName;
5455
} else {
5556
// This is for developers who mistakenly call this method with resource
5657
// bucket type, as for bucket type we need bucket and volumeName.

0 commit comments

Comments
 (0)