-
Notifications
You must be signed in to change notification settings - Fork 593
HDDS-13798. Implement PoolBasedHierarchicalResourceLockManager for Hierarchical Resource #9160
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
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
915562b
HDDS-13797. Refactor OzoneManagerLock Resource class to handle handle…
swamirishi 1c0d0ac
Merge remote-tracking branch 'apache/master' into HEAD
swamirishi 24da3eb
HDDS-13797. Update interface
swamirishi 8f3774a
HDDS-13798. Implement PoolBasedHierarchicalResourceLockManager for Hi…
swamirishi 6865fad
HDDS-13797. Revert move of Leveled Resource and Resource enum/interface
swamirishi 903ecd1
Merge remote-tracking branch 'origin/HDDS-13797' into HEAD
swamirishi 60a7728
Merge remote-tracking branch 'apache/master' into HEAD
swamirishi 4711517
HDDS-13798. Fix pmd findbugs
swamirishi 655a724
HDDS-13798. Fix pmd findbugs
swamirishi 2bc6134
HDDS-13798. Fix ozone-default.xml
swamirishi 8e8c534
HDDS-13798. Stop lock data manager on metadata stop
swamirishi f148f24
HDDS-13798. Update tests
swamirishi da030c0
HDDS-13798. Rename class
swamirishi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
204 changes: 204 additions & 0 deletions
204
...c/main/java/org/apache/hadoop/ozone/om/lock/PoolBasedHierarchicalResourceLockManager.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,204 @@ | ||
| /* | ||
| * 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 static org.apache.hadoop.ozone.om.OMConfigKeys.OZONE_OM_HIERARCHICAL_RESOURCE_LOCKS_HARD_LIMIT; | ||
| import static org.apache.hadoop.ozone.om.OMConfigKeys.OZONE_OM_HIERARCHICAL_RESOURCE_LOCKS_HARD_LIMIT_DEFAULT; | ||
| import static org.apache.hadoop.ozone.om.OMConfigKeys.OZONE_OM_HIERARCHICAL_RESOURCE_LOCKS_SOFT_LIMIT; | ||
| import static org.apache.hadoop.ozone.om.OMConfigKeys.OZONE_OM_HIERARCHICAL_RESOURCE_LOCKS_SOFT_LIMIT_DEFAULT; | ||
|
|
||
| import com.google.common.base.Preconditions; | ||
| import java.io.Closeable; | ||
| import java.io.IOException; | ||
| import java.util.Map; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
| import java.util.concurrent.atomic.AtomicReference; | ||
| import java.util.concurrent.locks.Lock; | ||
| import java.util.concurrent.locks.ReadWriteLock; | ||
| import java.util.concurrent.locks.ReentrantReadWriteLock; | ||
| import java.util.function.Consumer; | ||
| import org.apache.commons.pool2.BasePooledObjectFactory; | ||
| import org.apache.commons.pool2.PooledObject; | ||
| import org.apache.commons.pool2.impl.DefaultPooledObject; | ||
| import org.apache.commons.pool2.impl.GenericObjectPool; | ||
| import org.apache.commons.pool2.impl.GenericObjectPoolConfig; | ||
| import org.apache.hadoop.hdds.conf.OzoneConfiguration; | ||
|
|
||
| /** | ||
| * A lock manager implementation that manages hierarchical resource locks | ||
| * using a pool of reusable {@link ReadWriteLock} instances. The implementation | ||
| * ensures deterministic lock ordering for resources, avoiding cyclic | ||
| * lock dependencies, and is typically useful for structures like | ||
| * DAGs (e.g., File System trees or snapshot chains). | ||
| */ | ||
| public class PoolBasedHierarchicalResourceLockManager implements HierarchicalResourceLockManager { | ||
| private final GenericObjectPool<ReadWriteLock> lockPool; | ||
| private final Map<FlatResource, Map<String, LockReferenceCountPair>> lockMap; | ||
|
|
||
| public PoolBasedHierarchicalResourceLockManager(OzoneConfiguration conf) { | ||
| int softLimit = conf.getInt(OZONE_OM_HIERARCHICAL_RESOURCE_LOCKS_SOFT_LIMIT, | ||
| OZONE_OM_HIERARCHICAL_RESOURCE_LOCKS_SOFT_LIMIT_DEFAULT); | ||
| int hardLimit = conf.getInt(OZONE_OM_HIERARCHICAL_RESOURCE_LOCKS_HARD_LIMIT, | ||
| OZONE_OM_HIERARCHICAL_RESOURCE_LOCKS_HARD_LIMIT_DEFAULT); | ||
| GenericObjectPoolConfig<ReadWriteLock> config = new GenericObjectPoolConfig<>(); | ||
| config.setMaxIdle(softLimit); | ||
| config.setMaxTotal(hardLimit); | ||
| config.setBlockWhenExhausted(true); | ||
| this.lockPool = new GenericObjectPool<>(new ReadWriteLockFactory(), config); | ||
| this.lockMap = new ConcurrentHashMap<>(); | ||
| } | ||
|
|
||
| private ReadWriteLock operateOnLock(FlatResource resource, String key, Consumer<LockReferenceCountPair> function) | ||
| throws IOException { | ||
| AtomicReference<IOException> exception = new AtomicReference<>(); | ||
| Map<String, LockReferenceCountPair> resourceLockMap = | ||
| this.lockMap.computeIfAbsent(resource, k -> new ConcurrentHashMap<>()); | ||
| LockReferenceCountPair lockRef = resourceLockMap.compute(key, (k, v) -> { | ||
| if (v == null) { | ||
| try { | ||
| ReadWriteLock readWriteLock = this.lockPool.borrowObject(); | ||
| v = new LockReferenceCountPair(readWriteLock); | ||
| } catch (Exception e) { | ||
| exception.set(new IOException("Exception while initializing lock object.", e)); | ||
| return null; | ||
| } | ||
| } | ||
| function.accept(v); | ||
| Preconditions.checkState(v.getCount() >= 0); | ||
| if (v.getCount() == 0) { | ||
| this.lockPool.returnObject(v.getLock()); | ||
| return null; | ||
| } | ||
| return v; | ||
| }); | ||
| if (exception.get() != null) { | ||
| throw exception.get(); | ||
| } | ||
| return lockRef == null ? null : lockRef.getLock(); | ||
| } | ||
|
|
||
| @Override | ||
| public HierarchicalResourceLock acquireReadLock(FlatResource resource, String key) throws IOException { | ||
| return acquireLock(resource, key, true); | ||
| } | ||
|
|
||
| @Override | ||
| public HierarchicalResourceLock acquireWriteLock(FlatResource resource, String key) throws IOException { | ||
| return acquireLock(resource, key, false); | ||
| } | ||
|
|
||
| private HierarchicalResourceLock acquireLock(FlatResource resource, String key, boolean isReadLock) | ||
| throws IOException { | ||
| ReadWriteLock readWriteLock = operateOnLock(resource, key, LockReferenceCountPair::increment); | ||
| if (readWriteLock == null) { | ||
| throw new IOException("Unable to acquire " + (isReadLock ? "read" : "write") + " lock on resource " | ||
| + resource + " and key " + key); | ||
| } | ||
| return new PoolBasedHierarchicalResourceLock(resource, key, | ||
| isReadLock ? readWriteLock.readLock() : readWriteLock.writeLock()); | ||
| } | ||
|
|
||
| @Override | ||
| public void close() { | ||
| this.lockPool.close(); | ||
| } | ||
|
|
||
| /** | ||
| * Represents a hierarchical resource lock mechanism that operates | ||
| * using a resource pool for acquiring and releasing locks. This class | ||
| * provides thread-safe management of read and write locks associated | ||
| * with specific hierarchical resources. | ||
| * | ||
| * A lock can either be a read lock or a write lock. This is determined | ||
| * at the time of instantiation. The lifecycle of the lock is managed | ||
| * through this class, and the lock is automatically released when the | ||
| * `close` method is invoked. | ||
| * | ||
| * This is designed to work in conjunction with the containing manager | ||
| * class, {@code PoolBasedHierarchicalResourceLockManager}, which oversees | ||
| * the lifecycle of multiple such locks. | ||
| */ | ||
| public class PoolBasedHierarchicalResourceLock implements HierarchicalResourceLock, Closeable { | ||
|
|
||
| private boolean isLockAcquired; | ||
| private final Lock lock; | ||
| private final FlatResource resource; | ||
| private final String key; | ||
|
|
||
| public PoolBasedHierarchicalResourceLock(FlatResource resource, String key, Lock lock) { | ||
| this.isLockAcquired = true; | ||
| this.lock = lock; | ||
| this.resource = resource; | ||
| this.key = key; | ||
| this.lock.lock(); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isLockAcquired() { | ||
| return isLockAcquired; | ||
| } | ||
|
|
||
| @Override | ||
| public synchronized void close() throws IOException { | ||
| if (isLockAcquired) { | ||
| this.lock.unlock(); | ||
| operateOnLock(resource, key, (LockReferenceCountPair::decrement)); | ||
| isLockAcquired = false; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static final class LockReferenceCountPair { | ||
| private int count; | ||
| private ReadWriteLock lock; | ||
|
|
||
| private LockReferenceCountPair(ReadWriteLock lock) { | ||
| this.count = 0; | ||
| this.lock = lock; | ||
| } | ||
|
|
||
| private void increment() { | ||
| count++; | ||
| } | ||
|
|
||
| private void decrement() { | ||
| count--; | ||
| } | ||
|
|
||
| private int getCount() { | ||
| return count; | ||
| } | ||
|
|
||
| private ReadWriteLock getLock() { | ||
| return lock; | ||
| } | ||
| } | ||
|
|
||
| private static class ReadWriteLockFactory extends BasePooledObjectFactory<ReadWriteLock> { | ||
|
|
||
| @Override | ||
| public ReadWriteLock create() throws Exception { | ||
| return new ReentrantReadWriteLock(); | ||
| } | ||
|
|
||
| @Override | ||
| public PooledObject<ReadWriteLock> wrap(ReadWriteLock obj) { | ||
| return new DefaultPooledObject<>(obj); | ||
| } | ||
| } | ||
| } |
64 changes: 64 additions & 0 deletions
64
...rc/main/java/org/apache/hadoop/ozone/om/lock/ReadOnlyHierarchicalResourceLockManager.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| /* | ||
| * 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.io.IOException; | ||
|
|
||
| /** | ||
| * A read only lock manager that does not acquire any lock. | ||
| */ | ||
| public class ReadOnlyHierarchicalResourceLockManager implements HierarchicalResourceLockManager { | ||
|
|
||
| private static final HierarchicalResourceLock EMPTY_LOCK_ACQUIRED = new HierarchicalResourceLock() { | ||
| @Override | ||
| public boolean isLockAcquired() { | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| public void close() { | ||
|
|
||
| } | ||
| }; | ||
|
|
||
| private static final HierarchicalResourceLock EMPTY_LOCK_NOT_ACQUIRED = new HierarchicalResourceLock() { | ||
| @Override | ||
| public boolean isLockAcquired() { | ||
| return false; | ||
| } | ||
|
|
||
| @Override | ||
| public void close() { | ||
| } | ||
| }; | ||
|
|
||
| @Override | ||
| public HierarchicalResourceLock acquireReadLock(FlatResource resource, String key) throws IOException { | ||
| return EMPTY_LOCK_ACQUIRED; | ||
| } | ||
|
|
||
| @Override | ||
| public HierarchicalResourceLock acquireWriteLock(FlatResource resource, String key) throws IOException { | ||
| return EMPTY_LOCK_NOT_ACQUIRED; | ||
| } | ||
|
|
||
| @Override | ||
| public void close() throws Exception { | ||
|
|
||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i'm curious how this is going to be used because it looks basically like a no-op
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah this would be used when opening checkpoint metadata managers similar to OmReadOnlyLocks. Eventually we might want to use the HierarchicalLock for FSO kinda use case so created this along the same lines to OzoneManagerLock