adoroszlai commented on code in PR #4885: URL: https://github.com/apache/ozone/pull/4885#discussion_r1234718708
########## hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/utils/CompositeKey.java: ########## @@ -0,0 +1,69 @@ +/* + * 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.hdds.utils; + +import java.util.Arrays; + +/** + * This is a utility to combine multiple objects as a key that can be used in + * hash map access. The advantage of this is that it is cheap in comparison + * to other methods like string concatenation. + * + * For example, if a composition of volume, bucket and key is needed to + * access a hash map, the natural method is: + * <pre> {@code + * String key = "/" + volume + "/" + bucket + "/" + key. + * map.put(key, value); + * }</pre> + * This is costly because it creates (and stores) a new buffer. + * + * In comparison, the following achieve the same logic without creating any new + * buffer. + * <pre> {@code + * Object key = combineKeys(volume, bucket, key). + * map.put(key, value); + * }</pre> + * + */ +public class CompositeKey { Review Comment: Nit: ```suggestion public final class CompositeKey { ``` ########## hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/om/lock/OzoneManagerLock.java: ########## @@ -95,34 +97,35 @@ public class OzoneManagerLock implements IOzoneManagerLock { * @param conf Configuration object */ public OzoneManagerLock(ConfigurationSource conf) { - boolean fair = conf.getBoolean(OZONE_MANAGER_FAIR_LOCK, - OZONE_MANAGER_FAIR_LOCK_DEFAULT); - manager = new LockManager<>(conf, fair); omLockMetrics = OMLockMetrics.create(); + + // TODO: for now, guava Striped doesn't allow create a striped + // ReadWriteLock with fair-lock yet. + // https://github.com/google/guava/issues/2514. + // We may have to consider implement our own striped locks. + //boolean fair = conf.getBoolean(OZONE_MANAGER_FAIR_LOCK, + // OZONE_MANAGER_FAIR_LOCK_DEFAULT); + + Map<Resource, Striped<ReadWriteLock>> stripedLockMap = new HashMap<>(); + for (Resource r : Resource.values()) { + stripedLockMap.put(r, createStripeLock(r, conf)); + } + this.stripedLockByResource = Collections.unmodifiableMap(stripedLockMap); } - /** - * Acquire lock on resource. - * - * For S3_BUCKET_LOCK, VOLUME_LOCK, BUCKET_LOCK type resource, same - * thread acquiring lock again is allowed. - * - * For USER_LOCK, PREFIX_LOCK, S3_SECRET_LOCK type resource, same thread - * acquiring lock again is not allowed. - * - * Special Note for USER_LOCK: Single thread can acquire single user lock/ - * multi user lock. But not both at the same time. - * @param resource - Type of the resource. - * @param resources - Resource names on which user want to acquire lock. - * For Resource type BUCKET_LOCK, first param should be volume, second param - * should be bucket name. For remaining all resource only one param should - * be passed. - */ - @Override - @Deprecated - public boolean acquireLock(Resource resource, String... resources) { - String resourceName = generateResourceName(resource, resources); - return lock(resource, resourceName, manager::writeLock, WRITE_LOCK); + private Striped<ReadWriteLock> createStripeLock(Resource r, + ConfigurationSource conf) { + String stripeSizeKey = OZONE_MANAGER_STRIPED_LOCK_SIZE_PREFIX + + r.name().toLowerCase(); Review Comment: ```suggestion r.getName().toLowerCase(); ``` ########## hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/om/lock/OzoneManagerLock.java: ########## @@ -95,34 +97,35 @@ public class OzoneManagerLock implements IOzoneManagerLock { * @param conf Configuration object */ public OzoneManagerLock(ConfigurationSource conf) { - boolean fair = conf.getBoolean(OZONE_MANAGER_FAIR_LOCK, - OZONE_MANAGER_FAIR_LOCK_DEFAULT); - manager = new LockManager<>(conf, fair); omLockMetrics = OMLockMetrics.create(); + + // TODO: for now, guava Striped doesn't allow create a striped + // ReadWriteLock with fair-lock yet. + // https://github.com/google/guava/issues/2514. + // We may have to consider implement our own striped locks. + //boolean fair = conf.getBoolean(OZONE_MANAGER_FAIR_LOCK, + // OZONE_MANAGER_FAIR_LOCK_DEFAULT); + + Map<Resource, Striped<ReadWriteLock>> stripedLockMap = new HashMap<>(); Review Comment: Nit: change to `EnumMap`. ########## hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/om/lock/OzoneManagerLock.java: ########## @@ -95,34 +97,35 @@ public class OzoneManagerLock implements IOzoneManagerLock { * @param conf Configuration object */ public OzoneManagerLock(ConfigurationSource conf) { - boolean fair = conf.getBoolean(OZONE_MANAGER_FAIR_LOCK, - OZONE_MANAGER_FAIR_LOCK_DEFAULT); - manager = new LockManager<>(conf, fair); omLockMetrics = OMLockMetrics.create(); + + // TODO: for now, guava Striped doesn't allow create a striped + // ReadWriteLock with fair-lock yet. + // https://github.com/google/guava/issues/2514. + // We may have to consider implement our own striped locks. + //boolean fair = conf.getBoolean(OZONE_MANAGER_FAIR_LOCK, + // OZONE_MANAGER_FAIR_LOCK_DEFAULT); + + Map<Resource, Striped<ReadWriteLock>> stripedLockMap = new HashMap<>(); + for (Resource r : Resource.values()) { + stripedLockMap.put(r, createStripeLock(r, conf)); + } + this.stripedLockByResource = Collections.unmodifiableMap(stripedLockMap); } - /** - * Acquire lock on resource. - * - * For S3_BUCKET_LOCK, VOLUME_LOCK, BUCKET_LOCK type resource, same - * thread acquiring lock again is allowed. - * - * For USER_LOCK, PREFIX_LOCK, S3_SECRET_LOCK type resource, same thread - * acquiring lock again is not allowed. - * - * Special Note for USER_LOCK: Single thread can acquire single user lock/ - * multi user lock. But not both at the same time. - * @param resource - Type of the resource. - * @param resources - Resource names on which user want to acquire lock. - * For Resource type BUCKET_LOCK, first param should be volume, second param - * should be bucket name. For remaining all resource only one param should - * be passed. - */ - @Override - @Deprecated - public boolean acquireLock(Resource resource, String... resources) { - String resourceName = generateResourceName(resource, resources); - return lock(resource, resourceName, manager::writeLock, WRITE_LOCK); + private Striped<ReadWriteLock> createStripeLock(Resource r, + ConfigurationSource conf) { + String stripeSizeKey = OZONE_MANAGER_STRIPED_LOCK_SIZE_PREFIX + + r.name().toLowerCase(); + int size = conf.getInt(stripeSizeKey, + OZONE_MANAGER_STRIPED_LOCK_SIZE_DEFAULT); + return Striped.readWriteLock(size); + } + + private ReentrantReadWriteLock getLock(Resource resource, String... keys) { + Object key = keys.length == 1 ? keys[0] : combineKeys(keys); Review Comment: I think this logic is better moved to `combineKeys` to apply to other future usage of `CompositeKey`. -- 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. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
