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
*/
private void abortBlockInternal(long sessionId, long blockId) throws BlockDoesNotExistException, BlockAlreadyExistsException, InvalidWorkerStateException, IOException {
String path;
TempBlockMeta tempBlockMeta;
try (LockResource r = new LockResource(mMetadataReadLock)) {
checkTempBlockOwnedBySession(sessionId, blockId);
tempBlockMeta = mMetaManager.getTempBlockMeta(blockId);
path = tempBlockMeta.getPath();
}
// The metadata lock is released during heavy IO. The temp block is private to one session, so
// we do not lock it.
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);
}
}
use of alluxio.resource.LockResource in project alluxio by Alluxio.
the class TieredBlockStore method checkStorage.
@Override
public boolean checkStorage() {
try (LockResource r = new LockResource(mMetadataWriteLock)) {
List<StorageDir> dirsToRemove = new ArrayList<>();
for (StorageTier tier : mMetaManager.getTiers()) {
for (StorageDir dir : tier.getStorageDirs()) {
String path = dir.getDirPath();
if (!FileUtils.isStorageDirAccessible(path)) {
LOG.error("Storage check failed for path {}. The directory will be excluded.", path);
dirsToRemove.add(dir);
}
}
}
dirsToRemove.forEach(this::removeDir);
return !dirsToRemove.isEmpty();
}
}
use of alluxio.resource.LockResource in project alluxio by Alluxio.
the class TieredBlockStore method createBlockMetaInternal.
/**
* Creates a temp block meta only if allocator finds available space. This method will not trigger
* any eviction.
*
* @param sessionId session id
* @param blockId block id
* @param newBlock true if this temp block is created for a new block
* @param options block allocation options
* @return a temp block created if successful, or null if allocation failed (instead of throwing
* {@link WorkerOutOfSpaceException} because allocation failure could be an expected case)
* @throws BlockAlreadyExistsException if there is already a block with the same block id
*/
private TempBlockMeta createBlockMetaInternal(long sessionId, long blockId, boolean newBlock, AllocateOptions options) throws BlockAlreadyExistsException, WorkerOutOfSpaceException, IOException {
try (LockResource r = new LockResource(mMetadataWriteLock)) {
// unnecessary to acquire block lock here since no sharing.
if (newBlock) {
checkTempBlockIdAvailable(blockId);
}
// Allocate space.
StorageDirView dirView = allocateSpace(sessionId, options);
// TODO(carson): Add tempBlock to corresponding storageDir and remove the use of
// StorageDirView.createTempBlockMeta.
TempBlockMeta tempBlock = dirView.createTempBlockMeta(sessionId, blockId, options.getSize());
try {
// Add allocated temp block to metadata manager. This should never fail if allocator
// correctly assigns a StorageDir.
mMetaManager.addTempBlockMeta(tempBlock);
} catch (WorkerOutOfSpaceException | BlockAlreadyExistsException e) {
// If we reach here, allocator is not working properly
LOG.error("Unexpected failure: {} bytes allocated at {} by allocator, " + "but addTempBlockMeta failed", options.getSize(), options.getLocation());
throw Throwables.propagate(e);
}
return tempBlock;
}
}
use of alluxio.resource.LockResource in project alluxio by Alluxio.
the class TieredBlockStore method removeBlockInternal.
@VisibleForTesting
BlockMeta removeBlockInternal(long sessionId, long blockId, BlockStoreLocation location, long timeoutMs) throws InvalidWorkerStateException, BlockDoesNotExistException, IOException {
long lockId = mLockManager.tryLockBlock(sessionId, blockId, BlockLockType.WRITE, timeoutMs, TimeUnit.MILLISECONDS);
if (lockId == BlockWorker.INVALID_LOCK_ID) {
throw new DeadlineExceededException(String.format("Can not acquire lock to remove block %d for session %d after %d ms", blockId, sessionId, REMOVE_BLOCK_TIMEOUT_MS));
}
BlockMeta blockMeta;
try (LockResource r = new LockResource(mMetadataReadLock)) {
if (mMetaManager.hasTempBlockMeta(blockId)) {
throw new InvalidWorkerStateException(ExceptionMessage.REMOVE_UNCOMMITTED_BLOCK, blockId);
}
blockMeta = mMetaManager.getBlockMeta(blockId);
if (!blockMeta.getBlockLocation().belongsTo(location)) {
throw new BlockDoesNotExistException(ExceptionMessage.BLOCK_NOT_FOUND_AT_LOCATION, blockId, location);
}
} catch (Exception e) {
mLockManager.unlockBlock(lockId);
throw e;
}
try (LockResource r = new LockResource(mMetadataWriteLock)) {
removeBlockFileAndMeta(blockMeta);
} finally {
mLockManager.unlockBlock(lockId);
}
return blockMeta;
}
use of alluxio.resource.LockResource in project alluxio by Alluxio.
the class UnderFileSystemBlockStore method getBlockReader.
/**
* Creates a block reader that reads from UFS and optionally caches the block to the Alluxio
* block store.
*
* @param sessionId the client session ID that requested this read
* @param blockId the ID of the block to read
* @param offset the read offset within the block (NOT the file)
* @param positionShort whether the client op is a positioned read to a small buffer
* @param user the user that requests the block reader
* @return the block reader instance
* @throws BlockDoesNotExistException if the UFS block does not exist in the
* {@link UnderFileSystemBlockStore}
*/
public BlockReader getBlockReader(final long sessionId, long blockId, long offset, boolean positionShort, String user) throws BlockDoesNotExistException, IOException {
final BlockInfo blockInfo;
try (LockResource lr = new LockResource(mLock)) {
blockInfo = getBlockInfo(sessionId, blockId);
BlockReader blockReader = blockInfo.getBlockReader();
if (blockReader != null) {
return blockReader;
}
}
BlockReader reader = UnderFileSystemBlockReader.create(blockInfo.getMeta(), offset, positionShort, mLocalBlockStore, mUfsManager, mUfsInstreamCache, user);
blockInfo.setBlockReader(reader);
return reader;
}
Aggregations