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