Class FSNamesystemLock

java.lang.Object
org.apache.hadoop.hdfs.server.namenode.FSNamesystemLock

public class FSNamesystemLock extends Object
Mimics a ReentrantReadWriteLock but does not directly implement the interface so more sophisticated locking capabilities and logging/metrics are possible. DFSConfigKeys.DFS_NAMENODE_LOCK_DETAILED_METRICS_KEY to be true, metrics will be emitted into the FSNamesystem metrics registry for each operation which acquires this lock indicating how long the operation held the lock for. These metrics have names of the form ${LockName}(Read|Write)LockNanosOperationName, where OperationName denotes the name of the operation that initiated the lock hold (this will be OTHER for certain uncategorized operations) and they export the hold time values in nanoseconds. Note that if a thread dies, metrics produced after the most recent snapshot will be lost due to the use of MutableRatesWithAggregation. However since threads are re-used between operations this should not generally be an issue.
  • Field Details

  • Constructor Details

    • FSNamesystemLock

      public FSNamesystemLock(org.apache.hadoop.conf.Configuration conf, String lockName, org.apache.hadoop.metrics2.lib.MutableRatesWithAggregation detailedHoldTimeMetrics)
  • Method Details

    • readLock

      public void readLock()
    • readLockInterruptibly

      public void readLockInterruptibly() throws InterruptedException
      Throws:
      InterruptedException
    • readUnlock

      public void readUnlock()
    • readUnlock

      public void readUnlock(String opName)
    • readUnlock

      public void readUnlock(String opName, Supplier<String> lockReportInfoSupplier)
    • writeLock

      public void writeLock()
    • writeLockInterruptibly

      public void writeLockInterruptibly() throws InterruptedException
      Throws:
      InterruptedException
    • writeUnlock

      public void writeUnlock()
      Unlocks FSNameSystem write lock. This internally calls writeUnlock(String, boolean, Supplier)
    • writeUnlock

      public void writeUnlock(String opName)
      Unlocks FSNameSystem write lock. This internally calls writeUnlock(String, boolean, Supplier)
      Parameters:
      opName - Operation name.
    • writeUnlock

      public void writeUnlock(String opName, Supplier<String> lockReportInfoSupplier)
      Unlocks FSNameSystem write lock. This internally calls writeUnlock(String, boolean, Supplier)
      Parameters:
      opName - Operation name.
      lockReportInfoSupplier - The info shown in the lock report
    • writeUnlock

      public void writeUnlock(String opName, boolean suppressWriteLockReport)
      Unlocks FSNameSystem write lock. This internally calls writeUnlock(String, boolean, Supplier)
      Parameters:
      opName - Operation name.
      suppressWriteLockReport - When false, event of write lock being held for long time will be logged in logs and metrics.
    • getReadHoldCount

      public int getReadHoldCount()
    • getWriteHoldCount

      public int getWriteHoldCount()
    • isWriteLockedByCurrentThread

      public boolean isWriteLockedByCurrentThread()
    • newWriteLockCondition

      public Condition newWriteLockCondition()
    • getQueueLength

      public int getQueueLength()
      Returns the QueueLength of waiting threads. A larger number indicates greater lock contention.
      Returns:
      int - Number of threads waiting on this lock
    • getNumOfReadLockLongHold

      public long getNumOfReadLockLongHold()
      Returns the number of time the read lock has been held longer than the threshold.
      Returns:
      long - Number of time the read lock has been held longer than the threshold
    • getNumOfWriteLockLongHold

      public long getNumOfWriteLockLongHold()
      Returns the number of time the write lock has been held longer than the threshold.
      Returns:
      long - Number of time the write lock has been held longer than the threshold.
    • setMetricsEnabled

      @VisibleForTesting public void setMetricsEnabled(boolean metricsEnabled)
    • isMetricsEnabled

      public boolean isMetricsEnabled()
    • setReadLockReportingThresholdMs

      public void setReadLockReportingThresholdMs(long readLockReportingThresholdMs)
    • getReadLockReportingThresholdMs

      @VisibleForTesting public long getReadLockReportingThresholdMs()
    • setWriteLockReportingThresholdMs

      public void setWriteLockReportingThresholdMs(long writeLockReportingThresholdMs)
    • getWriteLockReportingThresholdMs

      @VisibleForTesting public long getWriteLockReportingThresholdMs()
    • setLockForTests

      public void setLockForTests(ReentrantReadWriteLock lock)
    • getLockForTests

      public ReentrantReadWriteLock getLockForTests()