errose28 commented on code in PR #8192: URL: https://github.com/apache/ozone/pull/8192#discussion_r2023773194
########## hadoop-ozone/ozone-manager/src/main/java/com/google/common/util/concurrent/StripedLock.java: ########## @@ -0,0 +1,54 @@ +/* + * 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 com.google.common.util.concurrent; + +import java.util.Comparator; +import java.util.concurrent.locks.ReentrantReadWriteLock; + +/** Locks obtained from {@link Striped}. */ +public class StripedLock<K> { Review Comment: This should be called `LockWithKey` or something similar. It is not a striped lock, which would contain multiple locks hashed by keys. ########## hadoop-ozone/ozone-manager/src/main/java/com/google/common/util/concurrent/StripedReadWriteLocks.java: ########## @@ -0,0 +1,63 @@ +/* + * 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 com.google.common.util.concurrent; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.TreeMap; +import java.util.concurrent.locks.ReentrantReadWriteLock; + +/** + * Similar to {@link Striped} except that + * the get methods in this class + * return {@link StripedLock} which includes the stripe index. + */ +public final class StripedReadWriteLocks { Review Comment: As far as I can tell this wrapper adds no extra functionality to Guava's `Striped<ReadWriteLock>`. What is its purpose? ########## hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/lock/granular/OmLockManager.java: ########## @@ -0,0 +1,94 @@ +/* + * 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.granular; + +import com.google.common.util.concurrent.StripedLock; +import com.google.common.util.concurrent.StripedReadWriteLocks; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import org.apache.hadoop.ozone.om.lock.granular.OmComponentLock.Component; +import org.apache.hadoop.ozone.om.lock.granular.OmComponentLock.Type; +import org.apache.ratis.util.UncheckedAutoCloseable; + +/** + * Manage locking of volume, bucket and keys. + */ +public class OmLockManager { + private final StripedReadWriteLocks volumeLocks = StripedReadWriteLocks.newInstance(1 << 10, true); + private final StripedReadWriteLocks bucketLocks = StripedReadWriteLocks.newInstance(1 << 12, true); + private final StripedReadWriteLocks keyLocks = StripedReadWriteLocks.newInstance(1 << 16, true); + + static OmComponentLock newOmComponentLock(Component component, Type type, StripedLock<String> lock) { + return new OmComponentLock(lock.getStripeKey(), component, type, lock.getIndex(), lock.getLock()); + } + + private OmComponentLock newVolumeLock(Type type, String name) { + return newOmComponentLock(Component.VOLUME, type, volumeLocks.get(name)); + } + + private OmComponentLock newBucketLock(Type type, String name) { + return newOmComponentLock(Component.BUCKET, type, bucketLocks.get(name)); + } + + private OmComponentLock newKeyLock(Type type, String name) { + return newOmComponentLock(Component.KEY, type, keyLocks.get(name)); + } + + private List<OmComponentLock> newKeyLocks(Type type, List<String> names) { + final List<OmComponentLock> list = new ArrayList<>(); + for(StripedLock<String> lock : keyLocks.bulkGet(names)) { + list.add(newOmComponentLock(Component.KEY, type, lock)); + } + return Collections.unmodifiableList(list); + } + + private OmOperationLock newVolumeReadBucketWriteLock(String volume, String bucket) { + return OmOperationLock.newInstance( + newVolumeLock(Type.READ, volume), + newBucketLock(Type.WRITE, bucket)); Review Comment: I don't think these wrappers are helpful. The content of the method is easier to understand than the method title. ########## hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/lock/granular/OmLockManager.java: ########## @@ -0,0 +1,94 @@ +/* + * 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.granular; + +import com.google.common.util.concurrent.StripedLock; +import com.google.common.util.concurrent.StripedReadWriteLocks; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import org.apache.hadoop.ozone.om.lock.granular.OmComponentLock.Component; +import org.apache.hadoop.ozone.om.lock.granular.OmComponentLock.Type; +import org.apache.ratis.util.UncheckedAutoCloseable; + +/** + * Manage locking of volume, bucket and keys. + */ +public class OmLockManager { + private final StripedReadWriteLocks volumeLocks = StripedReadWriteLocks.newInstance(1 << 10, true); + private final StripedReadWriteLocks bucketLocks = StripedReadWriteLocks.newInstance(1 << 12, true); + private final StripedReadWriteLocks keyLocks = StripedReadWriteLocks.newInstance(1 << 16, true); + + static OmComponentLock newOmComponentLock(Component component, Type type, StripedLock<String> lock) { + return new OmComponentLock(lock.getStripeKey(), component, type, lock.getIndex(), lock.getLock()); + } + + private OmComponentLock newVolumeLock(Type type, String name) { + return newOmComponentLock(Component.VOLUME, type, volumeLocks.get(name)); Review Comment: This is a strange implementation because this class already controls the locks. Now it is passing the same lock to multiple of these new lock objects. It would be more straightforward for this class to a handle the mapping of components to locks without splitting this responsibility across two classes which are sharing lock instances. -- 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]
