-
Notifications
You must be signed in to change notification settings - Fork 588
HDDS-11900. obs granular lock for existing request [To be splitted] #7647
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| /** | ||
| * 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 | ||
| * <p> | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * <p> | ||
| * 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 com.google.common.util.concurrent.Striped; | ||
| import java.io.IOException; | ||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.concurrent.locks.Lock; | ||
| import java.util.concurrent.locks.ReadWriteLock; | ||
| import org.apache.hadoop.ozone.om.exceptions.OMException; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| /** | ||
| * key locking. | ||
| */ | ||
| public class KeyLock { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this name is confusing. It seems to imply that it for locking "keys" as they are defined in the Ozone namespace. It is actually a generic class that supports locking based on any string, regardless of its significance to the system. I would move this to one of the common modules outside of OM, have it take any configurations as parameters, and throw general typed exceptions that upper layers can chain on to specific service type exceptions like |
||
| private static final Logger LOG = LoggerFactory.getLogger(KeyLock.class); | ||
| private static final long LOCK_TIMEOUT_DEFAULT = 10 * 60 * 1000; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's make stripe sizes and timeouts configurable. As suggested above though, this would happen in a layer above this class. |
||
| private final Striped<ReadWriteLock> fileStripedLock; | ||
| private final long lockTimeout; | ||
|
|
||
| public KeyLock(int stripLockSize) { | ||
| this(stripLockSize, LOCK_TIMEOUT_DEFAULT); | ||
| } | ||
|
|
||
| public KeyLock(int stripLockSize, long timeout) { | ||
| fileStripedLock = Striped.readWriteLock(stripLockSize); | ||
| lockTimeout = timeout; | ||
| } | ||
|
|
||
| public List<Lock> lock(List<String> keyList) throws IOException { | ||
| List<Lock> locks = new ArrayList<>(); | ||
| boolean isSuccess = false; | ||
| try { | ||
| Iterable<ReadWriteLock> readWriteLocks = fileStripedLock.bulkGet(keyList); | ||
| for (ReadWriteLock rwLock : readWriteLocks) { | ||
| Lock lockObj = rwLock.writeLock(); | ||
| boolean b = lockObj.tryLock(lockTimeout, TimeUnit.MILLISECONDS); | ||
| if (!b) { | ||
| LOG.error("Key write lock is failed for {} after wait of {}ms", this, lockTimeout); | ||
| throw new OMException("Unable to get write lock after " + lockTimeout + "ms" | ||
| + ", read lock info: " + rwLock.readLock(), | ||
| OMException.ResultCodes.TIMEOUT); | ||
| } | ||
| locks.add(lockObj); | ||
| } | ||
| isSuccess = true; | ||
| } catch (InterruptedException e) { | ||
| Thread.currentThread().interrupt(); | ||
| throw new OMException("Unable to get write lock as interrupted", OMException.ResultCodes.INTERNAL_ERROR); | ||
| } finally { | ||
| if (!isSuccess) { | ||
| Collections.reverse(locks); | ||
| locks.forEach(Lock::unlock); | ||
| locks.clear(); | ||
| } | ||
| } | ||
| return locks; | ||
| } | ||
|
|
||
| public Lock lock(String key) throws IOException { | ||
| LOG.debug("Key {} is locked for instance {} {}", key, this, fileStripedLock.get(key)); | ||
| try { | ||
| Lock lockObj = fileStripedLock.get(key).writeLock(); | ||
| boolean b = lockObj.tryLock(lockTimeout, TimeUnit.MILLISECONDS); | ||
| if (!b) { | ||
| LOG.error("Key {} lock is failed for {} after wait of {}ms", key, this, lockTimeout); | ||
| throw new OMException("Unable to get write lock for " + key + " after " + lockTimeout + "ms" | ||
| + ", read lock info: " + fileStripedLock.get(key).readLock(), | ||
| OMException.ResultCodes.TIMEOUT); | ||
| } | ||
| return lockObj; | ||
| } catch (InterruptedException e) { | ||
| Thread.currentThread().interrupt(); | ||
| throw new OMException("Unable to get read lock for " + key + " is interrupted", | ||
| OMException.ResultCodes.INTERNAL_ERROR); | ||
| } | ||
| } | ||
|
|
||
| public List<Lock> readLock(List<String> keyList) throws OMException { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need read lock? In the new flow RocksDB should handle consistent views without a cache. Is this just for compatibility with the old flow?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Readlock is added for bucket when key operation is in progress. Some key type need read lock and some need write lock, as as Generic part, the interface is provided. |
||
| List<Lock> locks = new ArrayList<>(); | ||
| boolean isSuccess = false; | ||
| try { | ||
| Iterable<ReadWriteLock> readWriteLocks = fileStripedLock.bulkGet(keyList); | ||
| for (ReadWriteLock rwLock : readWriteLocks) { | ||
| Lock lockObj = rwLock.readLock(); | ||
| boolean b = lockObj.tryLock(lockTimeout, TimeUnit.MILLISECONDS); | ||
| if (!b) { | ||
| LOG.error("Key read lock is failed for {} after wait of {}ms", this, lockTimeout); | ||
| throw new OMException("Unable to get read lock after " + lockTimeout + "ms" | ||
| + ", write lock info: " + rwLock.writeLock(), | ||
| OMException.ResultCodes.TIMEOUT); | ||
| } | ||
| locks.add(lockObj); | ||
| } | ||
| isSuccess = true; | ||
| } catch (InterruptedException e) { | ||
| Thread.currentThread().interrupt(); | ||
| throw new OMException("Unable to get read lock as interrupted", OMException.ResultCodes.INTERNAL_ERROR); | ||
| } finally { | ||
| if (!isSuccess) { | ||
| Collections.reverse(locks); | ||
| locks.forEach(Lock::unlock); | ||
| locks.clear(); | ||
| } | ||
| } | ||
| return locks; | ||
| } | ||
|
|
||
| public Lock readLock(String key) throws OMException { | ||
| try { | ||
| LOG.debug("Key {} is read locked for instance {} {}", key, this, fileStripedLock.get(key)); | ||
| Lock lockObj = fileStripedLock.get(key).readLock(); | ||
| boolean b = lockObj.tryLock(lockTimeout, TimeUnit.MILLISECONDS); | ||
| if (!b) { | ||
| throw new OMException("Unable to get read lock for " + key + " after " + lockTimeout + "ms", | ||
| OMException.ResultCodes.TIMEOUT); | ||
| } | ||
| return lockObj; | ||
| } catch (InterruptedException e) { | ||
| Thread.currentThread().interrupt(); | ||
| throw new OMException("Unable to get read lock for " + key + " is interrupted", | ||
| OMException.ResultCodes.INTERNAL_ERROR); | ||
| } | ||
| } | ||
| } | ||
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.
Please add a lot more detail to how this class is used