Search in sources :

Example 6 with LockBlockResult

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

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

the class AlluxioBlockStoreTest method getInStreamRemote.

/**
   * Tests {@link AlluxioBlockStore#getInStream(long, InStreamOptions)} when no local block
   * exists, making sure that the first {@link BlockLocation} in the {@link BlockInfo} list is
   * chosen.
   */
@Test
public void getInStreamRemote() throws Exception {
    Mockito.when(mMasterClient.getBlockInfo(BLOCK_ID)).thenReturn(new BlockInfo().setLocations(Arrays.asList(BLOCK_LOCATION_REMOTE)));
    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(RemoteBlockInStream.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)

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