Search in sources :

Example 41 with AlluxioException

use of alluxio.exception.AlluxioException in project alluxio by Alluxio.

the class LocalBlockInStream method create.

/**
   * Creates a new local block input stream.
   *
   * @param blockId the block id
   * @param blockSize the size of the block
   * @param workerNetAddress the address of the local worker
   * @param context the file system context
   * @param options the instream options
   * @return the {@link LocalBlockInStream} instance
   * @throws IOException if I/O error occurs
   */
// TODO(peis): Use options idiom (ALLUXIO-2579).
public static LocalBlockInStream create(long blockId, long blockSize, WorkerNetAddress workerNetAddress, FileSystemContext context, InStreamOptions options) throws IOException {
    Closer closer = Closer.create();
    try {
        BlockWorkerClient client = closer.register(context.createBlockWorkerClient(workerNetAddress));
        LockBlockResult result = closer.register(client.lockBlock(blockId, LockBlockOptions.defaults())).getResult();
        LocalFileBlockReader reader = closer.register(new LocalFileBlockReader(result.getBlockPath()));
        return new LocalBlockInStream(client, blockId, blockSize, reader, closer, options);
    } catch (AlluxioException | IOException e) {
        CommonUtils.closeQuitely(closer);
        throw CommonUtils.castToIOException(e);
    }
}
Also used : Closer(com.google.common.io.Closer) LocalFileBlockReader(alluxio.worker.block.io.LocalFileBlockReader) LockBlockResult(alluxio.wire.LockBlockResult) IOException(java.io.IOException) AlluxioException(alluxio.exception.AlluxioException)

Example 42 with AlluxioException

use of alluxio.exception.AlluxioException in project alluxio by Alluxio.

the class RemoteBlockInStream method create.

/**
   * Creates a new remote block input stream.
   *
   * @param blockId the block id
   * @param blockSize the block size
   * @param workerNetAddress the worker address
   * @param context the file system context to use for acquiring worker and master clients
   * @param options the instream options
   * @return the {@link RemoteBlockInStream} created
   * @throws IOException if the block is not available on the remote worker
   */
// TODO(peis): Use options idiom (ALLUXIO-2579).
public static RemoteBlockInStream create(long blockId, long blockSize, WorkerNetAddress workerNetAddress, FileSystemContext context, InStreamOptions options) throws IOException {
    Closer closer = Closer.create();
    try {
        BlockWorkerClient client = closer.register(context.createBlockWorkerClient(workerNetAddress));
        LockBlockResult result = closer.register(client.lockBlock(blockId, LockBlockOptions.defaults())).getResult();
        return new RemoteBlockInStream(context, client, blockId, blockSize, result.getLockId(), closer, options);
    } catch (AlluxioException | IOException e) {
        CommonUtils.closeQuitely(closer);
        throw CommonUtils.castToIOException(e);
    }
}
Also used : Closer(com.google.common.io.Closer) LockBlockResult(alluxio.wire.LockBlockResult) IOException(java.io.IOException) AlluxioException(alluxio.exception.AlluxioException)

Example 43 with AlluxioException

use of alluxio.exception.AlluxioException in project alluxio by Alluxio.

the class BlockInStream method createRemoteBlockInStream.

/**
   * Creates an instance of remote {@link BlockInStream} that reads from a remote worker.
   *
   * @param blockId the block ID
   * @param blockSize the block size
   * @param workerNetAddress the worker network address
   * @param context the file system context
   * @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 createRemoteBlockInStream(long blockId, long blockSize, WorkerNetAddress workerNetAddress, FileSystemContext context, InStreamOptions options) throws IOException {
    Closer closer = Closer.create();
    try {
        BlockWorkerClient blockWorkerClient = closer.register(context.createBlockWorkerClient(workerNetAddress));
        LockBlockResource lockBlockResource = closer.register(blockWorkerClient.lockBlock(blockId, LockBlockOptions.defaults()));
        PacketInStream inStream = closer.register(PacketInStream.createNettyPacketInStream(context, blockWorkerClient.getDataServerAddress(), blockId, lockBlockResource.getResult().getLockId(), blockWorkerClient.getSessionId(), blockSize, false, Protocol.RequestType.ALLUXIO_BLOCK));
        blockWorkerClient.accessBlock(blockId);
        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) LockBlockResource(alluxio.client.resource.LockBlockResource) BlockWorkerClient(alluxio.client.block.BlockWorkerClient) IOException(java.io.IOException) AlluxioException(alluxio.exception.AlluxioException)

Example 44 with AlluxioException

use of alluxio.exception.AlluxioException 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)

Example 45 with AlluxioException

use of alluxio.exception.AlluxioException in project alluxio by Alluxio.

the class AbstractFileSystem method create.

/**
   * Attempts to create a file. Overwrite will not succeed if the path exists and is a folder.
   *
   * @param path path to create
   * @param permission permissions of the created file/folder
   * @param overwrite overwrite if file exists
   * @param bufferSize the size in bytes of the buffer to be used
   * @param replication under filesystem replication factor
   * @param blockSize block size in bytes
   * @param progress queryable progress
   * @return an {@link FSDataOutputStream} created at the indicated path of a file
   * @throws IOException if overwrite is not specified and the path already exists or if the path is
   *         a folder
   */
@Override
public FSDataOutputStream create(Path path, FsPermission permission, boolean overwrite, int bufferSize, short replication, long blockSize, Progressable progress) throws IOException {
    LOG.debug("create({}, {}, {}, {}, {}, {}, {})", path, permission, overwrite, bufferSize, replication, blockSize, progress);
    if (mStatistics != null) {
        mStatistics.incrementWriteOps(1);
    }
    AlluxioURI uri = new AlluxioURI(HadoopUtils.getPathWithoutScheme(path));
    CreateFileOptions options = CreateFileOptions.defaults().setBlockSizeBytes(blockSize).setMode(new Mode(permission.toShort()));
    FileOutStream outStream;
    try {
        outStream = mFileSystem.createFile(uri, options);
    } catch (AlluxioException e) {
        //now we should consider the override parameter
        try {
            if (mFileSystem.exists(uri)) {
                if (!overwrite) {
                    throw new IOException(ExceptionMessage.FILE_ALREADY_EXISTS.getMessage(uri));
                }
                if (mFileSystem.getStatus(uri).isFolder()) {
                    throw new IOException(ExceptionMessage.FILE_CREATE_IS_DIRECTORY.getMessage(uri));
                }
                mFileSystem.delete(uri);
            }
            outStream = mFileSystem.createFile(uri, options);
        } catch (AlluxioException e2) {
            throw new IOException(e2);
        }
    }
    return new FSDataOutputStream(outStream, mStatistics);
}
Also used : CreateFileOptions(alluxio.client.file.options.CreateFileOptions) Mode(alluxio.security.authorization.Mode) FileOutStream(alluxio.client.file.FileOutStream) IOException(java.io.IOException) FSDataOutputStream(org.apache.hadoop.fs.FSDataOutputStream) AlluxioURI(alluxio.AlluxioURI) AlluxioException(alluxio.exception.AlluxioException)

Aggregations

AlluxioException (alluxio.exception.AlluxioException)56 IOException (java.io.IOException)54 AlluxioURI (alluxio.AlluxioURI)33 FileDoesNotExistException (alluxio.exception.FileDoesNotExistException)15 URIStatus (alluxio.client.file.URIStatus)13 ArrayList (java.util.ArrayList)11 InvalidPathException (alluxio.exception.InvalidPathException)9 Closer (com.google.common.io.Closer)8 FileOutStream (alluxio.client.file.FileOutStream)5 FileAlreadyExistsException (alluxio.exception.FileAlreadyExistsException)5 WorkerNetAddress (alluxio.wire.WorkerNetAddress)5 LockBlockResult (alluxio.wire.LockBlockResult)4 BlockWorkerClient (alluxio.client.block.BlockWorkerClient)3 Mode (alluxio.security.authorization.Mode)3 AlluxioTException (alluxio.thrift.AlluxioTException)3 ThriftIOException (alluxio.thrift.ThriftIOException)3 File (java.io.File)3 WorkerStorageTierAssoc (alluxio.WorkerStorageTierAssoc)2 LockBlockOptions (alluxio.client.block.options.LockBlockOptions)2 SetAttributeOptions (alluxio.client.file.options.SetAttributeOptions)2