use of alluxio.exception.BlockDoesNotExistException in project alluxio by Alluxio.
the class DefaultBlockWorker method createLocalBlockReader.
/**
* Creates the block reader to read the local cached block starting from given block offset.
* Owner of this block reader must close it or lock will leak.
*
* @param sessionId the id of the client
* @param blockId the id of the block to read
* @param offset the offset within this block
* @return the block reader for the block or null if block not found
*/
@Nullable
private BlockReader createLocalBlockReader(long sessionId, long blockId, long offset) throws IOException {
long lockId = mLocalBlockStore.lockBlockNoException(sessionId, blockId);
if (lockId == BlockWorker.INVALID_LOCK_ID) {
return null;
}
try {
BlockReader reader = mLocalBlockStore.getBlockReader(sessionId, blockId, lockId);
((FileChannel) reader.getChannel()).position(offset);
mLocalBlockStore.accessBlock(sessionId, blockId);
return new DelegatingBlockReader(reader, () -> {
try {
mLocalBlockStore.unlockBlock(lockId);
} catch (BlockDoesNotExistException e) {
throw new IOException(e);
}
});
} catch (Exception e) {
try {
mLocalBlockStore.unlockBlock(lockId);
} catch (Exception ee) {
LOG.warn("Failed to unlock block blockId={}, lockId={}", blockId, lockId, ee);
}
throw new IOException(String.format("Failed to get local block reader, sessionId=%d, " + "blockId=%d, offset=%d", sessionId, blockId, offset), e);
}
}
use of alluxio.exception.BlockDoesNotExistException in project alluxio by Alluxio.
the class DefaultBlockWorker method createUfsBlockReader.
@Override
public BlockReader createUfsBlockReader(long sessionId, long blockId, long offset, boolean positionShort, Protocol.OpenUfsBlockOptions options) throws BlockDoesNotExistException, IOException {
try {
openUfsBlock(sessionId, blockId, options);
BlockReader reader = mUnderFileSystemBlockStore.getBlockReader(sessionId, blockId, offset, positionShort, options.getUser());
return new DelegatingBlockReader(reader, () -> {
try {
closeUfsBlock(sessionId, blockId);
} catch (BlockAlreadyExistsException | IOException | WorkerOutOfSpaceException e) {
throw new IOException(e);
}
});
} catch (Exception e) {
try {
closeUfsBlock(sessionId, blockId);
} catch (Exception ee) {
LOG.warn("Failed to close UFS block", ee);
}
throw new IOException(String.format("Failed to read from UFS, sessionId=%d, " + "blockId=%d, offset=%d, positionShort=%s, options=%s: %s", sessionId, blockId, offset, positionShort, options, e.toString()), e);
}
}
use of alluxio.exception.BlockDoesNotExistException in project alluxio by Alluxio.
the class AlignTask method generateSwapTransferInfos.
private List<BlockTransferInfo> generateSwapTransferInfos(Pair<List<Long>, List<Long>> swapLists) {
if (LOG.isDebugEnabled()) {
LOG.debug("Generating transfer infos from swap lists.\n" + "Source list of size:{} : {}\nDestination list of size:{} : {}", swapLists.getFirst().size(), swapLists.getFirst().stream().map(Object::toString).collect(Collectors.joining(",")), swapLists.getSecond().size(), swapLists.getSecond().stream().map(Object::toString).collect(Collectors.joining(",")));
}
// Function that is used to map blockId to <blockId,location> pair.
Function<Long, Pair<Long, BlockStoreLocation>> blockToPairFunc = (blockId) -> {
try {
return new Pair(blockId, mEvictorView.getBlockMeta(blockId).getBlockLocation());
} catch (BlockDoesNotExistException e) {
LOG.warn("Failed to find location of a block:{}. Error: {}", blockId, e);
return new Pair(blockId, BlockStoreLocation.anyTier());
}
};
// Generate an augmented block lists with locations.
List<Pair<Long, BlockStoreLocation>> blockLocPairListSrc = swapLists.getFirst().stream().map(blockToPairFunc).collect(Collectors.toList());
List<Pair<Long, BlockStoreLocation>> blockLocPairListDst = swapLists.getSecond().stream().map(blockToPairFunc).collect(Collectors.toList());
// Sort augmented lists by location.
// This will help to generate the buckets by a linear sweep.
Comparator<Pair<Long, BlockStoreLocation>> comparator = (o1, o2) -> {
BlockStoreLocation loc1 = o1.getSecond();
BlockStoreLocation loc2 = o2.getSecond();
int tierComp = loc1.tierAlias().compareTo(loc2.tierAlias());
if (tierComp != 0) {
return tierComp;
} else {
return loc1.dir() - loc2.dir();
}
};
Collections.sort(blockLocPairListSrc, comparator);
Collections.sort(blockLocPairListDst, comparator);
if (LOG.isDebugEnabled()) {
LOG.debug("Generated and sorted augmented swap lists.\n" + "Source list of size:{} :\n ->{}\nDestination list of size:{} :\n ->{}", blockLocPairListSrc.size(), blockLocPairListSrc.stream().map(Object::toString).collect(Collectors.joining("\n ->")), blockLocPairListDst.size(), blockLocPairListDst.stream().map(Object::toString).collect(Collectors.joining("\n ->")));
}
// Build transfer infos using the sorted locations.
List<BlockTransferInfo> transferInfos = new ArrayList<>(blockLocPairListSrc.size());
for (int i = 0; i < blockLocPairListSrc.size(); i++) {
transferInfos.add(BlockTransferInfo.createSwap(blockLocPairListSrc.get(i).getSecond(), blockLocPairListSrc.get(i).getFirst(), blockLocPairListDst.get(i).getSecond(), blockLocPairListDst.get(i).getFirst()));
}
if (LOG.isDebugEnabled()) {
LOG.debug("Generated {} swap transfers: \n ->{}", transferInfos.size(), transferInfos.stream().map(Object::toString).collect(Collectors.joining(",\n ->")));
}
return transferInfos;
}
use of alluxio.exception.BlockDoesNotExistException in project alluxio by Alluxio.
the class TieredBlockStore method lockBlock.
@Override
public long lockBlock(long sessionId, long blockId) throws BlockDoesNotExistException {
LOG.debug("lockBlock: sessionId={}, blockId={}", sessionId, blockId);
long lockId = mLockManager.lockBlock(sessionId, blockId, BlockLockType.READ);
boolean hasBlock;
try (LockResource r = new LockResource(mMetadataReadLock)) {
hasBlock = mMetaManager.hasBlockMeta(blockId);
}
if (hasBlock) {
return lockId;
}
mLockManager.unlockBlock(lockId);
throw new BlockDoesNotExistException(ExceptionMessage.NO_BLOCK_ID_FOUND, blockId);
}
use of alluxio.exception.BlockDoesNotExistException 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 moveOptions the allocate options for the move
* @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
*/
private MoveBlockResult moveBlockInternal(long sessionId, long blockId, BlockStoreLocation oldLocation, AllocateOptions moveOptions) 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();
// Update moveOptions with the block size.
moveOptions.setSize(blockSize);
}
if (!srcLocation.belongsTo(oldLocation)) {
throw new BlockDoesNotExistException(ExceptionMessage.BLOCK_NOT_FOUND_AT_LOCATION, blockId, oldLocation);
}
if (srcLocation.belongsTo(moveOptions.getLocation())) {
return new MoveBlockResult(true, blockSize, srcLocation, srcLocation);
}
TempBlockMeta dstTempBlock;
try {
dstTempBlock = createBlockMetaInternal(sessionId, blockId, false, moveOptions);
} catch (Exception e) {
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);
}
}
Aggregations