use of alluxio.resource.LockResource in project alluxio by Alluxio.
the class BlockLockManager method lockBlockInternal.
private long lockBlockInternal(long sessionId, long blockId, BlockLockType blockLockType, boolean blocking, @Nullable Long time, @Nullable TimeUnit unit) {
ClientRWLock blockLock = getBlockLock(blockId);
Lock lock = blockLockType == BlockLockType.READ ? blockLock.readLock() : blockLock.writeLock();
// Make sure the session isn't already holding the block lock.
if (blockLockType == BlockLockType.WRITE && sessionHoldsLock(sessionId, blockId)) {
throw new IllegalStateException(String.format("Session %s attempted to take a write lock on block %s, but the session already" + " holds a lock on the block", sessionId, blockId));
}
if (blocking) {
lock.lock();
} else {
Preconditions.checkNotNull(time, "time");
Preconditions.checkNotNull(unit, "unit");
try {
if (!lock.tryLock(time, unit)) {
LOG.warn("Failed to acquire lock for block {} after {} {}. " + "session: {}, blockLockType: {}, lock reference count = {}", blockId, time, unit, sessionId, blockLockType, blockLock.getReferenceCount());
return BlockWorker.INVALID_LOCK_ID;
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return BlockWorker.INVALID_LOCK_ID;
}
}
try {
long lockId = LOCK_ID_GEN.getAndIncrement();
try (LockResource r = new LockResource(mSharedMapsLock.writeLock())) {
mLockIdToRecordMap.put(lockId, new LockRecord(sessionId, blockId, lock));
Set<Long> sessionLockIds = mSessionIdToLockIdsMap.get(sessionId);
if (sessionLockIds == null) {
mSessionIdToLockIdsMap.put(sessionId, Sets.newHashSet(lockId));
} else {
sessionLockIds.add(lockId);
}
}
return lockId;
} catch (Throwable e) {
// If an unexpected exception occurs, we should release the lock to be conservative.
unlock(lock, blockId);
throw e;
}
}
use of alluxio.resource.LockResource in project alluxio by Alluxio.
the class BlockLockManager method unlockBlock.
/**
* Releases the lock with the specified session and block id.
*
* @param sessionId the session id
* @param blockId the block id
* @return whether the block has been successfully unlocked
*/
// TODO(bin): Temporary, remove me later.
public boolean unlockBlock(long sessionId, long blockId) {
try (LockResource r = new LockResource(mSharedMapsLock.writeLock())) {
Set<Long> sessionLockIds = mSessionIdToLockIdsMap.get(sessionId);
if (sessionLockIds == null) {
return false;
}
for (long lockId : sessionLockIds) {
LockRecord record = mLockIdToRecordMap.get(lockId);
if (record == null) {
// TODO(peis): Should this be a check failure?
return false;
}
if (blockId == record.getBlockId()) {
mLockIdToRecordMap.remove(lockId);
sessionLockIds.remove(lockId);
if (sessionLockIds.isEmpty()) {
mSessionIdToLockIdsMap.remove(sessionId);
}
Lock lock = record.getLock();
unlock(lock, blockId);
return true;
}
}
return false;
}
}
use of alluxio.resource.LockResource in project alluxio by Alluxio.
the class BlockLockManager method releaseBlockLockIfUnused.
/**
* Checks whether anyone is using the block lock for the given block id, returning the lock to
* the lock pool if it is unused.
*
* @param blockId the block id for which to potentially release the block lock
*/
private void releaseBlockLockIfUnused(long blockId) {
try (LockResource r = new LockResource(mSharedMapsLock.writeLock())) {
ClientRWLock lock = mLocks.get(blockId);
if (lock == null) {
// Someone else probably released the block lock already.
return;
}
// If we were the last worker with a reference to the lock, clean it up.
if (lock.dropReference() == 0) {
mLocks.remove(blockId);
mLockPool.release(lock);
}
}
}
use of alluxio.resource.LockResource in project alluxio by Alluxio.
the class TieredBlockStore method accessBlock.
@Override
public void accessBlock(long sessionId, long blockId) throws BlockDoesNotExistException {
LOG.debug("accessBlock: sessionId={}, blockId={}", sessionId, blockId);
try (LockResource r = new LockResource(mMetadataReadLock)) {
BlockMeta blockMeta = mMetaManager.getBlockMeta(blockId);
for (BlockStoreEventListener listener : mBlockStoreEventListeners) {
synchronized (listener) {
listener.onAccessBlock(sessionId, blockId);
listener.onAccessBlock(sessionId, blockId, blockMeta.getBlockLocation());
}
}
}
}
use of alluxio.resource.LockResource in project alluxio by Alluxio.
the class TieredBlockStore method cleanupSession.
@Override
public void cleanupSession(long sessionId) {
LOG.debug("cleanupSession: sessionId={}", sessionId);
// Release all locks the session is holding.
mLockManager.cleanupSession(sessionId);
// Collect a list of temp blocks the given session owns and abort all of them with best effort
List<TempBlockMeta> tempBlocksToRemove;
try (LockResource r = new LockResource(mMetadataReadLock)) {
tempBlocksToRemove = mMetaManager.getSessionTempBlocks(sessionId);
}
for (TempBlockMeta tempBlockMeta : tempBlocksToRemove) {
try {
LOG.warn("Clean up expired temporary block {} from session {}.", tempBlockMeta.getBlockId(), sessionId);
abortBlockInternal(sessionId, tempBlockMeta.getBlockId());
} catch (Exception e) {
LOG.error("Failed to cleanup tempBlock {}", tempBlockMeta.getBlockId(), e);
}
}
}
Aggregations