Search in sources :

Example 1 with LockBlockResource

use of alluxio.client.resource.LockBlockResource 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 2 with LockBlockResource

use of alluxio.client.resource.LockBlockResource in project alluxio by Alluxio.

the class BlockInStream method createLocalBlockInStream.

/**
   * Creates an instance of local {@link BlockInStream} that reads from local 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 createLocalBlockInStream(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.createLocalPacketInstream(lockBlockResource.getResult().getBlockPath(), blockId, blockSize));
        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 3 with LockBlockResource

use of alluxio.client.resource.LockBlockResource in project alluxio by Alluxio.

the class RetryHandlingBlockWorkerClient method lockUfsBlock.

@Override
public LockBlockResource lockUfsBlock(final long blockId, final LockBlockOptions options) throws IOException, AlluxioException {
    int retryInterval = Constants.SECOND_MS;
    RetryPolicy retryPolicy = new TimeoutRetry(Configuration.getLong(PropertyKey.USER_UFS_BLOCK_OPEN_TIMEOUT_MS), retryInterval);
    do {
        LockBlockResource resource = lockBlock(blockId, options);
        if (resource.getResult().getLockBlockStatus().ufsTokenNotAcquired()) {
            LOG.debug("Failed to acquire a UFS read token because of contention for block {} with " + "LockBlockOptions {}", blockId, options);
        } else {
            return resource;
        }
    } while (retryPolicy.attemptRetry());
    throw new UfsBlockAccessTokenUnavailableException(ExceptionMessage.UFS_BLOCK_ACCESS_TOKEN_UNAVAILABLE, blockId, options.getUfsPath());
}
Also used : UfsBlockAccessTokenUnavailableException(alluxio.exception.UfsBlockAccessTokenUnavailableException) LockBlockResource(alluxio.client.resource.LockBlockResource) TimeoutRetry(alluxio.retry.TimeoutRetry) RetryPolicy(alluxio.retry.RetryPolicy)

Example 4 with LockBlockResource

use of alluxio.client.resource.LockBlockResource 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 5 with LockBlockResource

use of alluxio.client.resource.LockBlockResource 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

LockBlockResource (alluxio.client.resource.LockBlockResource)5 BlockWorkerClient (alluxio.client.block.BlockWorkerClient)2 AlluxioException (alluxio.exception.AlluxioException)2 BlockInfo (alluxio.wire.BlockInfo)2 LockBlockResult (alluxio.wire.LockBlockResult)2 Closer (com.google.common.io.Closer)2 File (java.io.File)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 Test (org.junit.Test)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 UfsBlockAccessTokenUnavailableException (alluxio.exception.UfsBlockAccessTokenUnavailableException)1 RetryPolicy (alluxio.retry.RetryPolicy)1 TimeoutRetry (alluxio.retry.TimeoutRetry)1