Search in sources :

Example 1 with LockBlockResult

use of alluxio.wire.LockBlockResult 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 LockBlockResult

use of alluxio.wire.LockBlockResult in project alluxio by Alluxio.

the class AlluxioBlockStoreTest method getInStreamLocal.

/**
   * Tests {@link AlluxioBlockStore#getInStream(long, InStreamOptions)} when a local block
   * exists, making sure that the local block is preferred.
   */
@Test
public void getInStreamLocal() throws Exception {
    Mockito.when(mMasterClient.getBlockInfo(BLOCK_ID)).thenReturn(new BlockInfo().setLocations(Arrays.asList(BLOCK_LOCATION_REMOTE, BLOCK_LOCATION_LOCAL)));
    File mTestFile = mTestFolder.newFile("testFile");
    // When a block lock for id BLOCK_ID is requested, a path to a temporary file is returned
    Mockito.when(mBlockWorkerClient.lockBlock(BLOCK_ID, LockBlockOptions.defaults())).thenReturn(new LockBlockResource(mBlockWorkerClient, new LockBlockResult().setLockId(LOCK_ID).setBlockPath(mTestFile.getAbsolutePath()), BLOCK_ID));
    InputStream stream = mBlockStore.getInStream(BLOCK_ID, InStreamOptions.defaults());
    if (Configuration.getBoolean(PropertyKey.USER_PACKET_STREAMING_ENABLED)) {
        Assert.assertEquals(alluxio.client.block.stream.BlockInStream.class, stream.getClass());
    } else {
        Assert.assertEquals(LocalBlockInStream.class, stream.getClass());
    }
}
Also used : LockBlockResource(alluxio.client.resource.LockBlockResource) BlockInfo(alluxio.wire.BlockInfo) LockBlockResult(alluxio.wire.LockBlockResult) InputStream(java.io.InputStream) File(java.io.File) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 3 with LockBlockResult

use of alluxio.wire.LockBlockResult in project alluxio by Alluxio.

the class BlockWorkerClientRestApiTest method lockBlock.

@Test
public void lockBlock() throws Exception {
    mBlockWorker.createBlock(SESSION_ID, BLOCK_ID, TIER_ALIAS, INITIAL_BYTES);
    mBlockWorker.commitBlock(SESSION_ID, BLOCK_ID);
    Map<String, String> params = new HashMap<>();
    params.put("blockId", Long.toString(BLOCK_ID));
    params.put("sessionId", Long.toString(SESSION_ID));
    String result = new TestCase(mHostname, mPort, getEndpoint(BlockWorkerClientRestServiceHandler.LOCK_BLOCK), params, HttpMethod.POST, null).call();
    LockBlockResult lockBlockResult = new ObjectMapper().readValue(result, LockBlockResult.class);
    Assert.assertTrue(lockBlockResult.getBlockPath().contains(Configuration.get(PropertyKey.WORKER_DATA_FOLDER)));
}
Also used : HashMap(java.util.HashMap) TestCase(alluxio.rest.TestCase) LockBlockResult(alluxio.wire.LockBlockResult) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test) RestApiTest(alluxio.rest.RestApiTest)

Example 4 with LockBlockResult

use of alluxio.wire.LockBlockResult 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 5 with LockBlockResult

use of alluxio.wire.LockBlockResult 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)

Aggregations

LockBlockResult (alluxio.wire.LockBlockResult)7 AlluxioException (alluxio.exception.AlluxioException)4 Closer (com.google.common.io.Closer)4 IOException (java.io.IOException)4 Test (org.junit.Test)3 LockBlockOptions (alluxio.client.block.options.LockBlockOptions)2 LockBlockResource (alluxio.client.resource.LockBlockResource)2 BlockInfo (alluxio.wire.BlockInfo)2 LocalFileBlockReader (alluxio.worker.block.io.LocalFileBlockReader)2 File (java.io.File)2 InputStream (java.io.InputStream)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 BlockWorkerClient (alluxio.client.block.BlockWorkerClient)1 RestApiTest (alluxio.rest.RestApiTest)1 TestCase (alluxio.rest.TestCase)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 HashMap (java.util.HashMap)1