Search in sources :

Example 1 with DelegatingBlockReader

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);
    }
}
Also used : FileChannel(java.nio.channels.FileChannel) BlockReader(alluxio.worker.block.io.BlockReader) DelegatingBlockReader(alluxio.worker.block.io.DelegatingBlockReader) DelegatingBlockReader(alluxio.worker.block.io.DelegatingBlockReader) IOException(java.io.IOException) BlockDoesNotExistException(alluxio.exception.BlockDoesNotExistException) WorkerOutOfSpaceException(alluxio.exception.WorkerOutOfSpaceException) AlluxioException(alluxio.exception.AlluxioException) UnavailableException(alluxio.exception.status.UnavailableException) InvalidWorkerStateException(alluxio.exception.InvalidWorkerStateException) BlockAlreadyExistsException(alluxio.exception.BlockAlreadyExistsException) IOException(java.io.IOException) BlockDoesNotExistException(alluxio.exception.BlockDoesNotExistException) Nullable(javax.annotation.Nullable)

Example 2 with DelegatingBlockReader

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);
    }
}
Also used : BlockAlreadyExistsException(alluxio.exception.BlockAlreadyExistsException) BlockReader(alluxio.worker.block.io.BlockReader) DelegatingBlockReader(alluxio.worker.block.io.DelegatingBlockReader) DelegatingBlockReader(alluxio.worker.block.io.DelegatingBlockReader) IOException(java.io.IOException) WorkerOutOfSpaceException(alluxio.exception.WorkerOutOfSpaceException) WorkerOutOfSpaceException(alluxio.exception.WorkerOutOfSpaceException) AlluxioException(alluxio.exception.AlluxioException) UnavailableException(alluxio.exception.status.UnavailableException) InvalidWorkerStateException(alluxio.exception.InvalidWorkerStateException) BlockAlreadyExistsException(alluxio.exception.BlockAlreadyExistsException) IOException(java.io.IOException) BlockDoesNotExistException(alluxio.exception.BlockDoesNotExistException)

Aggregations

AlluxioException (alluxio.exception.AlluxioException)2 BlockAlreadyExistsException (alluxio.exception.BlockAlreadyExistsException)2 BlockDoesNotExistException (alluxio.exception.BlockDoesNotExistException)2 InvalidWorkerStateException (alluxio.exception.InvalidWorkerStateException)2 WorkerOutOfSpaceException (alluxio.exception.WorkerOutOfSpaceException)2 UnavailableException (alluxio.exception.status.UnavailableException)2 BlockReader (alluxio.worker.block.io.BlockReader)2 DelegatingBlockReader (alluxio.worker.block.io.DelegatingBlockReader)2 IOException (java.io.IOException)2 FileChannel (java.nio.channels.FileChannel)1 Nullable (javax.annotation.Nullable)1