use of alluxio.resource.LockResource in project alluxio by Alluxio.
the class TieredBlockStore method commitBlockInternal.
/**
* Commits a temp block.
*
* @param sessionId the id of session
* @param blockId the id of block
* @param pinOnCreate is block pinned on create
* @return destination location to move the 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 BlockStoreLocation commitBlockInternal(long sessionId, long blockId, boolean pinOnCreate) throws BlockAlreadyExistsException, InvalidWorkerStateException, BlockDoesNotExistException, IOException {
// When committing TempBlockMeta, the final BlockMeta calculates the block size according to
// the actual file size of this TempBlockMeta. Therefore, commitTempBlockMeta must happen
// after moving actual block file to its committed path.
BlockStoreLocation loc;
String srcPath;
String dstPath;
TempBlockMeta tempBlockMeta;
try (LockResource r = new LockResource(mMetadataReadLock)) {
checkTempBlockOwnedBySession(sessionId, blockId);
tempBlockMeta = mMetaManager.getTempBlockMeta(blockId);
srcPath = tempBlockMeta.getPath();
dstPath = tempBlockMeta.getCommitPath();
loc = tempBlockMeta.getBlockLocation();
}
// Heavy IO is guarded by block lock but not metadata lock. This may throw IOException.
FileUtils.move(srcPath, dstPath);
try (LockResource r = new LockResource(mMetadataWriteLock)) {
mMetaManager.commitTempBlockMeta(tempBlockMeta);
} catch (BlockAlreadyExistsException | BlockDoesNotExistException | WorkerOutOfSpaceException e) {
// we shall never reach here
throw Throwables.propagate(e);
}
// Check if block is pinned on commit
if (pinOnCreate) {
addToPinnedInodes(BlockId.getFileId(blockId));
}
return loc;
}
use of alluxio.resource.LockResource in project alluxio by Alluxio.
the class TieredBlockStore method requestSpace.
@Override
public void requestSpace(long sessionId, long blockId, long additionalBytes) throws BlockDoesNotExistException, WorkerOutOfSpaceException, IOException {
LOG.debug("requestSpace: sessionId={}, blockId={}, additionalBytes={}", sessionId, blockId, additionalBytes);
if (additionalBytes <= 0) {
return;
}
// block lock here since no sharing
try (LockResource r = new LockResource(mMetadataWriteLock)) {
TempBlockMeta tempBlockMeta = mMetaManager.getTempBlockMeta(blockId);
StorageDirView allocationDir = allocateSpace(sessionId, AllocateOptions.forRequestSpace(additionalBytes, tempBlockMeta.getBlockLocation()));
if (!allocationDir.toBlockStoreLocation().equals(tempBlockMeta.getBlockLocation())) {
// If reached here, allocateSpace() failed to enforce 'forceLocation' flag.
throw new IllegalStateException(String.format("Allocation error: location enforcement failed for location: %s", allocationDir.toBlockStoreLocation()));
}
// Increase the size of this temp block
try {
mMetaManager.resizeTempBlockMeta(tempBlockMeta, tempBlockMeta.getBlockSize() + additionalBytes);
} catch (InvalidWorkerStateException e) {
// we shall never reach here
throw Throwables.propagate(e);
}
}
}
use of alluxio.resource.LockResource in project alluxio by Alluxio.
the class TieredBlockStore method getBlockMeta.
@Override
public BlockMeta getBlockMeta(long sessionId, long blockId, long lockId) throws BlockDoesNotExistException, InvalidWorkerStateException {
LOG.debug("getBlockMeta: sessionId={}, blockId={}, lockId={}", sessionId, blockId, lockId);
mLockManager.validateLock(sessionId, blockId, lockId);
try (LockResource r = new LockResource(mMetadataReadLock)) {
return mMetaManager.getBlockMeta(blockId);
}
}
use of alluxio.resource.LockResource in project alluxio by Alluxio.
the class UnderFileSystemBlockStore method acquireAccess.
/**
* Acquires access for a UFS block given a {@link UnderFileSystemBlockMeta} and the limit on
* the maximum concurrency on the block. If the number of concurrent readers on this UFS block
* exceeds a threshold, the token is not granted and this method returns false.
*
* @param sessionId the session ID
* @param blockId maximum concurrency
* @param options the options
* @return whether an access token is acquired
* @throws BlockAlreadyExistsException if the block already exists for a session ID
*/
public boolean acquireAccess(long sessionId, long blockId, Protocol.OpenUfsBlockOptions options) throws BlockAlreadyExistsException {
UnderFileSystemBlockMeta blockMeta = new UnderFileSystemBlockMeta(sessionId, blockId, options);
try (LockResource lr = new LockResource(mLock)) {
Key key = new Key(sessionId, blockId);
if (mBlocks.containsKey(key)) {
throw new BlockAlreadyExistsException(ExceptionMessage.UFS_BLOCK_ALREADY_EXISTS_FOR_SESSION, blockId, blockMeta.getUnderFileSystemPath(), sessionId);
}
Set<Long> sessionIds = mBlockIdToSessionIds.get(blockId);
if (sessionIds != null && sessionIds.size() >= options.getMaxUfsReadConcurrency()) {
return false;
}
if (sessionIds == null) {
sessionIds = new HashSet<>();
mBlockIdToSessionIds.put(blockId, sessionIds);
}
sessionIds.add(sessionId);
mBlocks.put(key, new BlockInfo(blockMeta));
Set<Long> blockIds = mSessionIdToBlockIds.get(sessionId);
if (blockIds == null) {
blockIds = new HashSet<>();
mSessionIdToBlockIds.put(sessionId, blockIds);
}
blockIds.add(blockId);
}
return true;
}
use of alluxio.resource.LockResource in project alluxio by Alluxio.
the class UnderFileSystemBlockStore method releaseAccess.
/**
* Releases the access token of this block by removing this (sessionId, blockId) pair from the
* store.
*
* @param sessionId the session ID
* @param blockId the block ID
*/
public void releaseAccess(long sessionId, long blockId) {
try (LockResource lr = new LockResource(mLock)) {
Key key = new Key(sessionId, blockId);
if (!mBlocks.containsKey(key)) {
LOG.warn("Key (block ID: {}, session ID {}) is not found when releasing the UFS block.", blockId, sessionId);
}
mBlocks.remove(key);
Set<Long> blockIds = mSessionIdToBlockIds.get(sessionId);
if (blockIds != null) {
blockIds.remove(blockId);
if (blockIds.isEmpty()) {
mSessionIdToBlockIds.remove(sessionId);
}
}
Set<Long> sessionIds = mBlockIdToSessionIds.get(blockId);
if (sessionIds != null) {
sessionIds.remove(sessionId);
if (sessionIds.isEmpty()) {
mBlockIdToSessionIds.remove(blockId);
}
}
}
}
Aggregations