use of alluxio.client.Cancelable in project alluxio by Alluxio.
the class JobUtils method loadBlock.
/**
* Loads a block into the local worker. If the block doesn't exist in Alluxio, it will be read
* from the UFS.
* @param status the uriStatus
* @param context filesystem context
* @param blockId the id of the block to load
* @param address specify a worker to load into
* @param directCache Use passive-cache or direct cache request
*/
public static void loadBlock(URIStatus status, FileSystemContext context, long blockId, WorkerNetAddress address, boolean directCache) throws AlluxioException, IOException {
AlluxioConfiguration conf = ServerConfiguration.global();
// Explicitly specified a worker to load
WorkerNetAddress localNetAddress = address;
String localHostName = NetworkAddressUtils.getConnectHost(ServiceType.WORKER_RPC, conf);
List<WorkerNetAddress> netAddress = context.getCachedWorkers().stream().map(BlockWorkerInfo::getNetAddress).filter(x -> Objects.equals(x.getHost(), localHostName)).collect(Collectors.toList());
if (localNetAddress == null && !netAddress.isEmpty()) {
localNetAddress = netAddress.get(0);
}
if (localNetAddress == null) {
throw new NotFoundException(ExceptionMessage.NO_LOCAL_BLOCK_WORKER_LOAD_TASK.getMessage(blockId));
}
Set<String> pinnedLocation = status.getPinnedMediumTypes();
if (pinnedLocation.size() > 1) {
throw new AlluxioException(ExceptionMessage.PINNED_TO_MULTIPLE_MEDIUMTYPES.getMessage(status.getPath()));
}
// Only use this read local first method to load if nearest worker is clear
if (netAddress.size() <= 1 && pinnedLocation.isEmpty() && status.isPersisted()) {
if (directCache) {
loadThroughCacheRequest(status, context, blockId, conf, localNetAddress);
} else {
loadThroughRead(status, context, blockId, conf);
}
return;
}
// TODO(bin): remove the following case when we consolidate tier and medium
// since there is only one element in the set, we take the first element in the set
String medium = pinnedLocation.isEmpty() ? "" : pinnedLocation.iterator().next();
OpenFilePOptions openOptions = OpenFilePOptions.newBuilder().setReadType(ReadPType.NO_CACHE).build();
InStreamOptions inOptions = new InStreamOptions(status, openOptions, conf);
// Set read location policy always to local first for loading blocks for job tasks
inOptions.setUfsReadLocationPolicy(BlockLocationPolicy.Factory.create(LocalFirstPolicy.class.getCanonicalName(), conf));
OutStreamOptions outOptions = OutStreamOptions.defaults(context.getClientContext());
outOptions.setMediumType(medium);
// Set write location policy always to local first for loading blocks for job tasks
outOptions.setLocationPolicy(BlockLocationPolicy.Factory.create(LocalFirstPolicy.class.getCanonicalName(), conf));
BlockInfo blockInfo = status.getBlockInfo(blockId);
Preconditions.checkNotNull(blockInfo, "Can not find block %s in status %s", blockId, status);
long blockSize = blockInfo.getLength();
AlluxioBlockStore blockStore = AlluxioBlockStore.create(context);
try (OutputStream outputStream = blockStore.getOutStream(blockId, blockSize, localNetAddress, outOptions)) {
try (InputStream inputStream = blockStore.getInStream(blockId, inOptions)) {
ByteStreams.copy(inputStream, outputStream);
} catch (Throwable t) {
try {
((Cancelable) outputStream).cancel();
} catch (Throwable t2) {
t.addSuppressed(t2);
}
throw t;
}
}
}
Aggregations