use of alluxio.worker.block.io.DelegatingBlockReader 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.worker.block.io.DelegatingBlockReader 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);
}
}
Aggregations