-
Notifications
You must be signed in to change notification settings - Fork 593
HDDS-13905. Bootstrap lock acquired in background services can lead to deadlock #9273
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
7bc2df0
83d623b
cf8cd8c
1b79a1c
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 |
|---|---|---|
|
|
@@ -17,28 +17,31 @@ | |
|
|
||
| package org.apache.hadoop.ozone.lock; | ||
|
|
||
| import java.util.concurrent.Semaphore; | ||
| import java.util.concurrent.locks.ReadWriteLock; | ||
| import java.util.concurrent.locks.ReentrantReadWriteLock; | ||
| import org.apache.ratis.util.UncheckedAutoCloseable; | ||
|
|
||
| /** Bootstrap state handler interface. */ | ||
| public interface BootstrapStateHandler { | ||
| Lock getBootstrapStateLock(); | ||
|
|
||
| /** Bootstrap state handler lock implementation. */ | ||
| class Lock implements AutoCloseable { | ||
| private final Semaphore semaphore = new Semaphore(1); | ||
| /** Bootstrap state handler lock implementation. Should be always acquired before opening any snapshot to avoid | ||
| * deadlocks*/ | ||
| class Lock { | ||
| private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock(); | ||
|
|
||
| public Lock lock() throws InterruptedException { | ||
| semaphore.acquire(); | ||
| return this; | ||
| private UncheckedAutoCloseable lock(boolean readLock) { | ||
| java.util.concurrent.locks.Lock lock = readLock ? readWriteLock.readLock() : readWriteLock.writeLock(); | ||
| lock.lock(); | ||
| return lock::unlock; | ||
|
Comment on lines
+35
to
+36
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. This line returns a method reference to unlock the acquired lock. Specifically, it creates an UncheckedAutoCloseable instance that, when closed, calls unlock() on the same lock, releasing it. This enables usage of the lock in a try-with-resources style, ensuring the lock will be released when no longer needed. |
||
| } | ||
|
|
||
| public void unlock() { | ||
| semaphore.release(); | ||
| public UncheckedAutoCloseable acquireWriteLock() throws InterruptedException { | ||
| return lock(false); | ||
| } | ||
|
|
||
| @Override | ||
| public void close() { | ||
| unlock(); | ||
| public UncheckedAutoCloseable acquireReadLock() throws InterruptedException { | ||
| return lock(true); | ||
|
Comment on lines
+39
to
+44
|
||
| } | ||
| } | ||
| } | ||
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.
Missing space after period in the comment. Should be "Should be always acquired before opening any snapshot to avoid deadlocks."