Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
}

Expand All @@ -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) {
Copy link
Contributor

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...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds good, done

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;
}
}

/**
Expand Down Expand Up @@ -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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pull the number into a constant. Know that I automatically -1 all inline constants in production code and save time all round.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my bad, let me fix this real quick

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();
Expand Down