-
Notifications
You must be signed in to change notification settings - Fork 9.2k
HDFS-17398. [FGL] Implement the FGL lock for FSNLockManager #6599
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
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
300 changes: 300 additions & 0 deletions
300
...src/main/java/org/apache/hadoop/hdfs/server/namenode/fgl/FineGrainedFSNamesystemLock.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,300 @@ | ||
| /** | ||
| * 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.hdfs.server.namenode.fgl; | ||
|
|
||
| import org.apache.hadoop.conf.Configuration; | ||
| import org.apache.hadoop.hdfs.server.namenode.FSNamesystemLock; | ||
| import org.apache.hadoop.metrics2.lib.MutableRatesWithAggregation; | ||
|
|
||
| import java.util.concurrent.locks.ReentrantReadWriteLock; | ||
| import java.util.function.Supplier; | ||
|
|
||
| /** | ||
| * Splitting the global FSN lock into FSLock and BMLock. | ||
| * FSLock is used to protect directory tree-related operations. | ||
| * BMLock is used to protect block-related and dn-related operations. | ||
| * The lock order should be: FSLock,BMLock. | ||
| */ | ||
| public class FineGrainedFSNamesystemLock implements FSNLockManager { | ||
| private final FSNamesystemLock fsLock; | ||
| private final FSNamesystemLock bmLock; | ||
|
|
||
| public FineGrainedFSNamesystemLock(Configuration conf, MutableRatesWithAggregation aggregation) { | ||
| this.fsLock = new FSNamesystemLock(conf, aggregation); | ||
| this.bmLock = new FSNamesystemLock(conf, aggregation); | ||
| } | ||
|
|
||
| @Override | ||
| public void readLock(FSNamesystemLockMode lockMode) { | ||
| if (lockMode.equals(FSNamesystemLockMode.GLOBAL)) { | ||
| this.fsLock.readLock(); | ||
| this.bmLock.readLock(); | ||
| } else if (lockMode.equals(FSNamesystemLockMode.FS)) { | ||
| this.fsLock.readLock(); | ||
| } else if (lockMode.equals(FSNamesystemLockMode.BM)) { | ||
| this.bmLock.readLock(); | ||
| } | ||
| } | ||
|
|
||
| public void readLockInterruptibly(FSNamesystemLockMode lockMode) throws InterruptedException { | ||
| if (lockMode.equals(FSNamesystemLockMode.GLOBAL)) { | ||
| this.fsLock.readLockInterruptibly(); | ||
| try { | ||
| this.bmLock.readLockInterruptibly(); | ||
| } catch (InterruptedException e) { | ||
| // The held FSLock should be released if the current thread is interrupted | ||
| // while acquiring the BMLock. | ||
| this.fsLock.readUnlock("BMReadLockInterruptiblyFailed"); | ||
| throw e; | ||
| } | ||
| } else if (lockMode.equals(FSNamesystemLockMode.FS)) { | ||
| this.fsLock.readLockInterruptibly(); | ||
| } else if (lockMode.equals(FSNamesystemLockMode.BM)) { | ||
| this.bmLock.readLockInterruptibly(); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void readUnlock(FSNamesystemLockMode lockMode, String opName) { | ||
| if (lockMode.equals(FSNamesystemLockMode.GLOBAL)) { | ||
| this.bmLock.readUnlock(opName); | ||
| this.fsLock.readUnlock(opName); | ||
| } else if (lockMode.equals(FSNamesystemLockMode.FS)) { | ||
| this.fsLock.readUnlock(opName); | ||
| } else if (lockMode.equals(FSNamesystemLockMode.BM)) { | ||
| this.bmLock.readUnlock(opName); | ||
| } | ||
| } | ||
|
|
||
| public void readUnlock(FSNamesystemLockMode lockMode, String opName, | ||
| Supplier<String> lockReportInfoSupplier) { | ||
| if (lockMode.equals(FSNamesystemLockMode.GLOBAL)) { | ||
| this.bmLock.readUnlock(opName, lockReportInfoSupplier); | ||
| this.fsLock.readUnlock(opName, lockReportInfoSupplier); | ||
| } else if (lockMode.equals(FSNamesystemLockMode.FS)) { | ||
| this.fsLock.readUnlock(opName, lockReportInfoSupplier); | ||
| } else if (lockMode.equals(FSNamesystemLockMode.BM)) { | ||
| this.bmLock.readUnlock(opName, lockReportInfoSupplier); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void writeLock(FSNamesystemLockMode lockMode) { | ||
| if (lockMode.equals(FSNamesystemLockMode.GLOBAL)) { | ||
| this.fsLock.writeLock(); | ||
| this.bmLock.writeLock(); | ||
| } else if (lockMode.equals(FSNamesystemLockMode.FS)) { | ||
| this.fsLock.writeLock(); | ||
| } else if (lockMode.equals(FSNamesystemLockMode.BM)) { | ||
| this.bmLock.writeLock(); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void writeUnlock(FSNamesystemLockMode lockMode, String opName) { | ||
| if (lockMode.equals(FSNamesystemLockMode.GLOBAL)) { | ||
| this.bmLock.writeUnlock(opName); | ||
| this.fsLock.writeUnlock(opName); | ||
| } else if (lockMode.equals(FSNamesystemLockMode.FS)) { | ||
| this.fsLock.writeUnlock(opName); | ||
| } else if (lockMode.equals(FSNamesystemLockMode.BM)) { | ||
| this.bmLock.writeUnlock(opName); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void writeUnlock(FSNamesystemLockMode lockMode, String opName, | ||
| boolean suppressWriteLockReport) { | ||
| if (lockMode.equals(FSNamesystemLockMode.GLOBAL)) { | ||
| this.bmLock.writeUnlock(opName, suppressWriteLockReport); | ||
| this.fsLock.writeUnlock(opName, suppressWriteLockReport); | ||
| } else if (lockMode.equals(FSNamesystemLockMode.FS)) { | ||
| this.fsLock.writeUnlock(opName, suppressWriteLockReport); | ||
| } else if (lockMode.equals(FSNamesystemLockMode.BM)) { | ||
| this.bmLock.writeUnlock(opName, suppressWriteLockReport); | ||
| } | ||
| } | ||
|
|
||
| public void writeUnlock(FSNamesystemLockMode lockMode, String opName, | ||
| Supplier<String> lockReportInfoSupplier) { | ||
| if (lockMode.equals(FSNamesystemLockMode.GLOBAL)) { | ||
| this.bmLock.writeUnlock(opName, lockReportInfoSupplier); | ||
| this.fsLock.writeUnlock(opName, lockReportInfoSupplier); | ||
| } else if (lockMode.equals(FSNamesystemLockMode.FS)) { | ||
| this.fsLock.writeUnlock(opName, lockReportInfoSupplier); | ||
| } else if (lockMode.equals(FSNamesystemLockMode.BM)) { | ||
| this.bmLock.writeUnlock(opName, lockReportInfoSupplier); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void writeLockInterruptibly(FSNamesystemLockMode lockMode) | ||
| throws InterruptedException { | ||
| if (lockMode.equals(FSNamesystemLockMode.GLOBAL)) { | ||
| this.fsLock.writeLockInterruptibly(); | ||
| try { | ||
| this.bmLock.writeLockInterruptibly(); | ||
| } catch (InterruptedException e) { | ||
| // The held FSLock should be released if the current thread is interrupted | ||
| // while acquiring the BMLock. | ||
| this.fsLock.writeUnlock("BMWriteLockInterruptiblyFailed"); | ||
| throw e; | ||
| } | ||
| } else if (lockMode.equals(FSNamesystemLockMode.FS)) { | ||
| this.fsLock.writeLockInterruptibly(); | ||
| } else if (lockMode.equals(FSNamesystemLockMode.BM)) { | ||
| this.bmLock.writeLockInterruptibly(); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public boolean hasWriteLock(FSNamesystemLockMode lockMode) { | ||
| if (lockMode.equals(FSNamesystemLockMode.GLOBAL)) { | ||
| if (this.fsLock.isWriteLockedByCurrentThread()) { | ||
| // The bm writeLock should be held by the current thread. | ||
| assert this.bmLock.isWriteLockedByCurrentThread(); | ||
| return true; | ||
| } else { | ||
| // The bm writeLock should not be held by the current thread. | ||
| assert !this.bmLock.isWriteLockedByCurrentThread(); | ||
| return false; | ||
| } | ||
| } else if (lockMode.equals(FSNamesystemLockMode.FS)) { | ||
| return this.fsLock.isWriteLockedByCurrentThread(); | ||
| } else if (lockMode.equals(FSNamesystemLockMode.BM)) { | ||
| return this.bmLock.isWriteLockedByCurrentThread(); | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean hasReadLock(FSNamesystemLockMode lockMode) { | ||
| if (lockMode.equals(FSNamesystemLockMode.GLOBAL)) { | ||
| if (hasWriteLock(FSNamesystemLockMode.GLOBAL)) { | ||
| return true; | ||
| } else if (this.fsLock.getReadHoldCount() > 0) { | ||
| // The bm readLock should be held by the current thread. | ||
| assert this.bmLock.getReadHoldCount() > 0; | ||
| return true; | ||
| } else { | ||
| // The bm readLock should not be held by the current thread. | ||
| assert this.bmLock.getReadHoldCount() <= 0; | ||
| return false; | ||
| } | ||
| } else if (lockMode.equals(FSNamesystemLockMode.FS)) { | ||
| return this.fsLock.getReadHoldCount() > 0 || this.fsLock.isWriteLockedByCurrentThread(); | ||
| } else if (lockMode.equals(FSNamesystemLockMode.BM)) { | ||
| return this.bmLock.getReadHoldCount() > 0 || this.bmLock.isWriteLockedByCurrentThread(); | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| @Override | ||
| /** | ||
| * This method is only used for ComputeDirectoryContentSummary. | ||
| * For the GLOBAL mode, just return the FSLock's ReadHoldCount. | ||
| */ | ||
| public int getReadHoldCount(FSNamesystemLockMode lockMode) { | ||
| if (lockMode.equals(FSNamesystemLockMode.GLOBAL)) { | ||
| return this.fsLock.getReadHoldCount(); | ||
| } else if (lockMode.equals(FSNamesystemLockMode.FS)) { | ||
| return this.fsLock.getReadHoldCount(); | ||
| } else if (lockMode.equals(FSNamesystemLockMode.BM)) { | ||
| return this.bmLock.getReadHoldCount(); | ||
| } | ||
| return -1; | ||
| } | ||
|
|
||
| @Override | ||
| public int getQueueLength(FSNamesystemLockMode lockMode) { | ||
| if (lockMode.equals(FSNamesystemLockMode.GLOBAL)) { | ||
| return -1; | ||
| } else if (lockMode.equals(FSNamesystemLockMode.FS)) { | ||
| return this.fsLock.getQueueLength(); | ||
| } else if (lockMode.equals(FSNamesystemLockMode.BM)) { | ||
| return this.bmLock.getQueueLength(); | ||
| } | ||
| return -1; | ||
| } | ||
|
|
||
| @Override | ||
| public long getNumOfReadLockLongHold(FSNamesystemLockMode lockMode) { | ||
| if (lockMode.equals(FSNamesystemLockMode.GLOBAL)) { | ||
| return -1; | ||
| } else if (lockMode.equals(FSNamesystemLockMode.FS)) { | ||
| return this.fsLock.getNumOfReadLockLongHold(); | ||
| } else if (lockMode.equals(FSNamesystemLockMode.BM)) { | ||
| return this.bmLock.getNumOfReadLockLongHold(); | ||
| } | ||
| return -1; | ||
| } | ||
|
|
||
| @Override | ||
| public long getNumOfWriteLockLongHold(FSNamesystemLockMode lockMode) { | ||
| if (lockMode.equals(FSNamesystemLockMode.GLOBAL)) { | ||
| return -1; | ||
| } else if (lockMode.equals(FSNamesystemLockMode.FS)) { | ||
| return this.fsLock.getNumOfWriteLockLongHold(); | ||
| } else if (lockMode.equals(FSNamesystemLockMode.BM)) { | ||
| return this.bmLock.getNumOfWriteLockLongHold(); | ||
| } | ||
| return -1; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isMetricsEnabled() { | ||
| return this.fsLock.isMetricsEnabled(); | ||
| } | ||
|
|
||
| public void setMetricsEnabled(boolean metricsEnabled) { | ||
| this.fsLock.setMetricsEnabled(metricsEnabled); | ||
| this.bmLock.setMetricsEnabled(metricsEnabled); | ||
| } | ||
|
|
||
| @Override | ||
| public void setReadLockReportingThresholdMs(long readLockReportingThresholdMs) { | ||
| this.fsLock.setReadLockReportingThresholdMs(readLockReportingThresholdMs); | ||
| this.bmLock.setReadLockReportingThresholdMs(readLockReportingThresholdMs); | ||
| } | ||
|
|
||
| @Override | ||
| public long getReadLockReportingThresholdMs() { | ||
| return this.fsLock.getReadLockReportingThresholdMs(); | ||
| } | ||
|
|
||
| @Override | ||
| public void setWriteLockReportingThresholdMs(long writeLockReportingThresholdMs) { | ||
| this.fsLock.setWriteLockReportingThresholdMs(writeLockReportingThresholdMs); | ||
| this.bmLock.setWriteLockReportingThresholdMs(writeLockReportingThresholdMs); | ||
| } | ||
|
|
||
| @Override | ||
| public long getWriteLockReportingThresholdMs() { | ||
| return this.fsLock.getWriteLockReportingThresholdMs(); | ||
| } | ||
|
|
||
| @Override | ||
| public void setLockForTests(ReentrantReadWriteLock lock) { | ||
| throw new UnsupportedOperationException("SetLockTests is unsupported"); | ||
| } | ||
|
|
||
| @Override | ||
| public ReentrantReadWriteLock getLockForTests() { | ||
| throw new UnsupportedOperationException("SetLockTests is unsupported"); | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.