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());
}
}
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);
}
}
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());
}
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);
}
}
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());
}
}
Aggregations