-
Notifications
You must be signed in to change notification settings - Fork 9.2k
HADOOP-18740. S3A prefetch cache blocks should be accessed by RW locks #5675
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 1 commit
912403c
33d64a4
710441f
d2b9210
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 |
|---|---|---|
|
|
@@ -37,6 +37,7 @@ | |
| import java.util.Map; | ||
| import java.util.Set; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.concurrent.locks.ReentrantReadWriteLock; | ||
|
|
||
| import org.apache.hadoop.thirdparty.com.google.common.collect.ImmutableSet; | ||
|
|
@@ -114,9 +115,9 @@ public String toString() { | |
| */ | ||
| void takeLock(LockType lockType) { | ||
| if (LockType.READ == lockType) { | ||
| this.lock.readLock().lock(); | ||
| lock.readLock().lock(); | ||
| } else if (LockType.WRITE == lockType) { | ||
| this.lock.writeLock().lock(); | ||
| lock.writeLock().lock(); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -127,11 +128,33 @@ void takeLock(LockType lockType) { | |
| */ | ||
| void releaseLock(LockType lockType) { | ||
| if (LockType.READ == lockType) { | ||
| this.lock.readLock().unlock(); | ||
| lock.readLock().unlock(); | ||
| } else if (LockType.WRITE == lockType) { | ||
| this.lock.writeLock().unlock(); | ||
| lock.writeLock().unlock(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Try to take the read or write lock within the given timeout. | ||
| * | ||
| * @param lockType type of the lock. | ||
| * @param timeout the time to wait for the given lock. | ||
| * @param unit the time unit of the timeout argument. | ||
| * @return true if the lock of the given lock type was acquired. | ||
| */ | ||
| boolean takeLock(LockType lockType, long timeout, TimeUnit unit) { | ||
| try { | ||
| if (LockType.READ == lockType) { | ||
| return lock.readLock().tryLock(timeout, unit); | ||
| } else if (LockType.WRITE == lockType) { | ||
| return lock.writeLock().tryLock(timeout, unit); | ||
| } | ||
| } catch (InterruptedException e) { | ||
| LOG.warn("Thread interrupted while trying to acquire {} lock", lockType, e); | ||
| Thread.currentThread().interrupt(); | ||
| } | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -310,7 +333,12 @@ public void close() throws IOException { | |
| int numFilesDeleted = 0; | ||
|
|
||
| for (Entry entry : blocks.values()) { | ||
| entry.takeLock(Entry.LockType.WRITE); | ||
| boolean lockAcquired = entry.takeLock(Entry.LockType.WRITE, 5, TimeUnit.SECONDS); | ||
|
||
| if (!lockAcquired) { | ||
| LOG.error("Cache file {} deletion would not be attempted as write lock could not" | ||
| + " be acquired within 5 sec", entry.path); | ||
| continue; | ||
| } | ||
| try { | ||
| Files.deleteIfExists(entry.path); | ||
| prefetchingStatistics.blockRemovedFromFileCache(); | ||
|
|
||
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.
should this and the others be private? you don't want other classes playing with your lock code...
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.
sounds good, done