use of alluxio.resource.LockResource in project alluxio by Alluxio.
the class TieredBlockStore method abortBlockInternal.
/**
* Aborts a temp block.
*
* @param sessionId the id of session
* @param blockId the id of block
* @throws BlockDoesNotExistException if block id can not be found in temporary blocks
* @throws BlockAlreadyExistsException if block id already exists in committed blocks
* @throws InvalidWorkerStateException if block id is not owned by session id
* @throws IOException if I/O errors occur when deleting the block file
*/
private void abortBlockInternal(long sessionId, long blockId) throws BlockDoesNotExistException, BlockAlreadyExistsException, InvalidWorkerStateException, IOException {
long lockId = mLockManager.lockBlock(sessionId, blockId, BlockLockType.WRITE);
try {
String path;
TempBlockMeta tempBlockMeta;
try (LockResource r = new LockResource(mMetadataReadLock)) {
checkTempBlockOwnedBySession(sessionId, blockId);
tempBlockMeta = mMetaManager.getTempBlockMeta(blockId);
path = tempBlockMeta.getPath();
}
// Heavy IO is guarded by block lock but not metadata lock. This may throw IOException.
Files.delete(Paths.get(path));
try (LockResource r = new LockResource(mMetadataWriteLock)) {
mMetaManager.abortTempBlockMeta(tempBlockMeta);
} catch (BlockDoesNotExistException e) {
// We shall never reach here
throw Throwables.propagate(e);
}
} finally {
mLockManager.unlockBlock(lockId);
}
}
use of alluxio.resource.LockResource in project alluxio by Alluxio.
the class TieredBlockStore method moveBlockInternal.
/**
* Moves a block to new location only if allocator finds available space in newLocation. This
* method will not trigger any eviction. Returns {@link MoveBlockResult}.
*
* @param sessionId session Id
* @param blockId block Id
* @param oldLocation the source location of the block
* @param newLocation new location to move this block
* @return the resulting information about the move operation
* @throws BlockDoesNotExistException if block is not found
* @throws BlockAlreadyExistsException if a block with same Id already exists in new location
* @throws InvalidWorkerStateException if the block to move is a temp block
* @throws IOException if I/O errors occur when moving block file
*/
private MoveBlockResult moveBlockInternal(long sessionId, long blockId, BlockStoreLocation oldLocation, BlockStoreLocation newLocation) throws BlockDoesNotExistException, BlockAlreadyExistsException, InvalidWorkerStateException, IOException {
long lockId = mLockManager.lockBlock(sessionId, blockId, BlockLockType.WRITE);
try {
long blockSize;
String srcFilePath;
String dstFilePath;
BlockMeta srcBlockMeta;
BlockStoreLocation srcLocation;
BlockStoreLocation dstLocation;
try (LockResource r = new LockResource(mMetadataReadLock)) {
if (mMetaManager.hasTempBlockMeta(blockId)) {
throw new InvalidWorkerStateException(ExceptionMessage.MOVE_UNCOMMITTED_BLOCK, blockId);
}
srcBlockMeta = mMetaManager.getBlockMeta(blockId);
srcLocation = srcBlockMeta.getBlockLocation();
srcFilePath = srcBlockMeta.getPath();
blockSize = srcBlockMeta.getBlockSize();
}
if (!srcLocation.belongsTo(oldLocation)) {
throw new BlockDoesNotExistException(ExceptionMessage.BLOCK_NOT_FOUND_AT_LOCATION, blockId, oldLocation);
}
TempBlockMeta dstTempBlock = createBlockMetaInternal(sessionId, blockId, newLocation, blockSize, false);
if (dstTempBlock == null) {
return new MoveBlockResult(false, blockSize, null, null);
}
// When `newLocation` is some specific location, the `newLocation` and the `dstLocation` are
// just the same; while for `newLocation` with a wildcard significance, the `dstLocation`
// is a specific one with specific tier and dir which belongs to newLocation.
dstLocation = dstTempBlock.getBlockLocation();
// internally from the newLocation and return success with specific block location.
if (dstLocation.belongsTo(srcLocation)) {
mMetaManager.abortTempBlockMeta(dstTempBlock);
return new MoveBlockResult(true, blockSize, srcLocation, dstLocation);
}
dstFilePath = dstTempBlock.getCommitPath();
// Heavy IO is guarded by block lock but not metadata lock. This may throw IOException.
FileUtils.move(srcFilePath, dstFilePath);
try (LockResource r = new LockResource(mMetadataWriteLock)) {
// If this metadata update fails, we panic for now.
// TODO(bin): Implement rollback scheme to recover from IO failures.
mMetaManager.moveBlockMeta(srcBlockMeta, dstTempBlock);
} catch (BlockAlreadyExistsException | BlockDoesNotExistException | WorkerOutOfSpaceException e) {
// we shall never reach here
throw Throwables.propagate(e);
}
return new MoveBlockResult(true, blockSize, srcLocation, dstLocation);
} finally {
mLockManager.unlockBlock(lockId);
}
}
use of alluxio.resource.LockResource in project alluxio by Alluxio.
the class HeartbeatScheduler method schedule.
/**
* Schedules execution of a heartbeat for the given thread.
*
* @param threadName a name of the thread for which heartbeat is to be executed
*/
public static void schedule(String threadName) {
try (LockResource r = new LockResource(sLock)) {
ScheduledTimer timer = sTimers.get(threadName);
if (timer == null) {
throw new RuntimeException("Timer for thread " + threadName + " not found.");
}
timer.schedule();
}
}
use of alluxio.resource.LockResource in project alluxio by Alluxio.
the class ScheduledTimer method tick.
/**
* Waits until the heartbeat is scheduled for execution.
*
* @throws InterruptedException if the thread is interrupted while waiting
*/
public void tick() throws InterruptedException {
try (LockResource r = new LockResource(mLock)) {
HeartbeatScheduler.addTimer(this);
// Wait in a loop to handle spurious wakeups
while (!mScheduled) {
mTickCondition.await();
}
mScheduled = false;
}
}
Aggregations