Search in sources :

Example 1 with LockBlockOptions

use of alluxio.client.block.options.LockBlockOptions in project alluxio by Alluxio.

the class UnderFileSystemBlockInStream method create.

/**
   * Creates an instance of {@link UnderFileSystemBlockInStream}.
   * This method keeps polling the block worker until the block is cached to Alluxio or
   * it successfully acquires a UFS read token with a timeout.
   * (1) If the block is cached to Alluxio after polling, it returns {@link BufferedBlockInStream}
   *     to read the block from Alluxio storage.
   * (2) If a UFS read token is acquired after polling, it returns
   *     {@link UnderFileSystemBlockInStream} to read the block from an Alluxio worker that reads
   *     the block from UFS.
   * (3) If the polling times out, an {@link IOException} with cause
   *     {@link alluxio.exception.UfsBlockAccessTokenUnavailableException} is thrown.
   *
   * @param context the file system context
   * @param ufsPath the UFS path
   * @param blockId the block ID
   * @param blockSize the block size
   * @param blockStart the start position of the block in the UFS file
   * @param workerNetAddress the worker network address
   * @param options the in stream options
   * @return the {@link UnderFileSystemBlockInStream} instance or the {@link BufferedBlockOutStream}
   *         that reads from Alluxio directly
   * @throws IOException if it fails to create {@link UnderFileSystemBlockInStream}
   */
public static BufferedBlockInStream create(FileSystemContext context, String ufsPath, long blockId, long blockSize, long blockStart, WorkerNetAddress workerNetAddress, InStreamOptions options) throws IOException {
    Closer closer = Closer.create();
    try {
        BlockWorkerClient blockWorkerClient = closer.register(context.createBlockWorkerClient(workerNetAddress));
        LockBlockOptions lockBlockOptions = LockBlockOptions.defaults().setUfsPath(ufsPath).setOffset(blockStart).setBlockSize(blockSize).setMaxUfsReadConcurrency(options.getMaxUfsReadConcurrency());
        LockBlockResult result = closer.register(blockWorkerClient.lockUfsBlock(blockId, lockBlockOptions)).getResult();
        if (result.getLockBlockStatus().blockInAlluxio()) {
            boolean local = blockWorkerClient.getDataServerAddress().getHostName().equals(NetworkAddressUtils.getLocalHostName());
            if (local) {
                LocalFileBlockReader reader = closer.register(new LocalFileBlockReader(result.getBlockPath()));
                return LocalBlockInStream.createWithLockedBlock(blockWorkerClient, blockId, blockSize, reader, closer, options);
            } else {
                return RemoteBlockInStream.createWithLockedBlock(context, blockWorkerClient, blockId, blockSize, result.getLockId(), closer, options);
            }
        }
        Preconditions.checkState(result.getLockBlockStatus().ufsTokenAcquired());
        return new UnderFileSystemBlockInStream(context, blockId, blockSize, blockWorkerClient, closer, options);
    } catch (AlluxioException | IOException e) {
        CommonUtils.closeQuitely(closer);
        throw CommonUtils.castToIOException(e);
    }
}
Also used : Closer(com.google.common.io.Closer) LockBlockOptions(alluxio.client.block.options.LockBlockOptions) LocalFileBlockReader(alluxio.worker.block.io.LocalFileBlockReader) LockBlockResult(alluxio.wire.LockBlockResult) IOException(java.io.IOException) AlluxioException(alluxio.exception.AlluxioException)

Example 2 with LockBlockOptions

use of alluxio.client.block.options.LockBlockOptions in project alluxio by Alluxio.

the class BlockInStream method createUfsBlockInStream.

/**
   * Creates an instance of {@link BlockInStream}.
   *
   * This method keeps polling the block worker until the block is cached to Alluxio or
   * it successfully acquires a UFS read token with a timeout.
   * (1) If the block is cached to Alluxio after polling, it returns {@link BlockInStream}
   *     to read the block from Alluxio storage.
   * (2) If a UFS read token is acquired after polling, it returns {@link BlockInStream}
   *     to read the block from an Alluxio worker that reads the block from UFS.
   * (3) If the polling times out, an {@link IOException} with cause
   *     {@link alluxio.exception.UfsBlockAccessTokenUnavailableException} is thrown.
   *
   * @param context the file system context
   * @param ufsPath the UFS path
   * @param blockId the block ID
   * @param blockSize the block size
   * @param blockStart the position at which the block starts in the file
   * @param workerNetAddress the worker network address
   * @param options the options
   * @throws IOException if it fails to create an instance
   * @return the {@link BlockInStream} created
   */
// TODO(peis): Use options idiom (ALLUXIO-2579).
public static BlockInStream createUfsBlockInStream(FileSystemContext context, String ufsPath, long blockId, long blockSize, long blockStart, WorkerNetAddress workerNetAddress, InStreamOptions options) throws IOException {
    Closer closer = Closer.create();
    try {
        BlockWorkerClient blockWorkerClient = closer.register(context.createBlockWorkerClient(workerNetAddress));
        LockBlockOptions lockBlockOptions = LockBlockOptions.defaults().setUfsPath(ufsPath).setOffset(blockStart).setBlockSize(blockSize).setMaxUfsReadConcurrency(options.getMaxUfsReadConcurrency());
        LockBlockResult lockBlockResult = closer.register(blockWorkerClient.lockUfsBlock(blockId, lockBlockOptions)).getResult();
        PacketInStream inStream;
        if (lockBlockResult.getLockBlockStatus().blockInAlluxio()) {
            boolean local = blockWorkerClient.getDataServerAddress().getHostName().equals(NetworkAddressUtils.getLocalHostName());
            if (local) {
                inStream = closer.register(PacketInStream.createLocalPacketInstream(lockBlockResult.getBlockPath(), blockId, blockSize));
            } else {
                inStream = closer.register(PacketInStream.createNettyPacketInStream(context, blockWorkerClient.getDataServerAddress(), blockId, lockBlockResult.getLockId(), blockWorkerClient.getSessionId(), blockSize, false, Protocol.RequestType.ALLUXIO_BLOCK));
            }
            blockWorkerClient.accessBlock(blockId);
        } else {
            Preconditions.checkState(lockBlockResult.getLockBlockStatus().ufsTokenAcquired());
            inStream = closer.register(PacketInStream.createNettyPacketInStream(context, blockWorkerClient.getDataServerAddress(), blockId, lockBlockResult.getLockId(), blockWorkerClient.getSessionId(), blockSize, !options.getAlluxioStorageType().isStore(), Protocol.RequestType.UFS_BLOCK));
        }
        return new BlockInStream(inStream, blockWorkerClient, closer, options);
    } catch (AlluxioException | IOException e) {
        CommonUtils.closeQuitely(closer);
        throw CommonUtils.castToIOException(e);
    }
}
Also used : Closer(com.google.common.io.Closer) LockBlockOptions(alluxio.client.block.options.LockBlockOptions) LockBlockResult(alluxio.wire.LockBlockResult) BlockWorkerClient(alluxio.client.block.BlockWorkerClient) IOException(java.io.IOException) AlluxioException(alluxio.exception.AlluxioException)

Aggregations

LockBlockOptions (alluxio.client.block.options.LockBlockOptions)2 AlluxioException (alluxio.exception.AlluxioException)2 LockBlockResult (alluxio.wire.LockBlockResult)2 Closer (com.google.common.io.Closer)2 IOException (java.io.IOException)2 BlockWorkerClient (alluxio.client.block.BlockWorkerClient)1 LocalFileBlockReader (alluxio.worker.block.io.LocalFileBlockReader)1