Search in sources :

Example 1 with Cancelable

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;
        }
    }
}
Also used : Cancelable(alluxio.client.Cancelable) BlockLocationPolicy(alluxio.client.block.policy.BlockLocationPolicy) WorkerNetAddress(alluxio.wire.WorkerNetAddress) BlockInfo(alluxio.wire.BlockInfo) BlockWorkerInfo(alluxio.client.block.BlockWorkerInfo) NetworkAddressUtils(alluxio.util.network.NetworkAddressUtils) FileBlockInfo(alluxio.wire.FileBlockInfo) PropertyKey(alluxio.conf.PropertyKey) ConcurrentMap(java.util.concurrent.ConcurrentMap) LocalFirstPolicy(alluxio.client.block.policy.LocalFirstPolicy) Constants(alluxio.Constants) CloseableResource(alluxio.resource.CloseableResource) AlluxioConfiguration(alluxio.conf.AlluxioConfiguration) ReadPType(alluxio.grpc.ReadPType) IndexDefinition(alluxio.collections.IndexDefinition) ServiceType(alluxio.util.network.NetworkAddressUtils.ServiceType) BlockWorkerClient(alluxio.client.block.stream.BlockWorkerClient) OutputStream(java.io.OutputStream) Protocol(alluxio.proto.dataserver.Protocol) IndexedSet(alluxio.collections.IndexedSet) ServerConfiguration(alluxio.conf.ServerConfiguration) ImmutableMap(com.google.common.collect.ImmutableMap) CacheRequest(alluxio.grpc.CacheRequest) BlockInStream(alluxio.client.block.stream.BlockInStream) InStreamOptions(alluxio.client.file.options.InStreamOptions) ExceptionMessage(alluxio.exception.ExceptionMessage) OutStreamOptions(alluxio.client.file.options.OutStreamOptions) Set(java.util.Set) AlluxioException(alluxio.exception.AlluxioException) IOException(java.io.IOException) OpenFilePOptions(alluxio.grpc.OpenFilePOptions) Pair(alluxio.collections.Pair) AlluxioBlockStore(alluxio.client.block.AlluxioBlockStore) NotFoundException(alluxio.exception.status.NotFoundException) Maps(com.google.common.collect.Maps) Collectors(java.util.stream.Collectors) AlluxioProperties(alluxio.conf.AlluxioProperties) Objects(java.util.Objects) BlockLocation(alluxio.wire.BlockLocation) URIStatus(alluxio.client.file.URIStatus) List(java.util.List) FileSystemContext(alluxio.client.file.FileSystemContext) ByteStreams(com.google.common.io.ByteStreams) Preconditions(com.google.common.base.Preconditions) InstancedConfiguration(alluxio.conf.InstancedConfiguration) InputStream(java.io.InputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) NotFoundException(alluxio.exception.status.NotFoundException) AlluxioConfiguration(alluxio.conf.AlluxioConfiguration) InStreamOptions(alluxio.client.file.options.InStreamOptions) OutStreamOptions(alluxio.client.file.options.OutStreamOptions) WorkerNetAddress(alluxio.wire.WorkerNetAddress) BlockInfo(alluxio.wire.BlockInfo) FileBlockInfo(alluxio.wire.FileBlockInfo) BlockWorkerInfo(alluxio.client.block.BlockWorkerInfo) OpenFilePOptions(alluxio.grpc.OpenFilePOptions) AlluxioBlockStore(alluxio.client.block.AlluxioBlockStore) AlluxioException(alluxio.exception.AlluxioException)

Aggregations

Constants (alluxio.Constants)1 Cancelable (alluxio.client.Cancelable)1 AlluxioBlockStore (alluxio.client.block.AlluxioBlockStore)1 BlockWorkerInfo (alluxio.client.block.BlockWorkerInfo)1 BlockLocationPolicy (alluxio.client.block.policy.BlockLocationPolicy)1 LocalFirstPolicy (alluxio.client.block.policy.LocalFirstPolicy)1 BlockInStream (alluxio.client.block.stream.BlockInStream)1 BlockWorkerClient (alluxio.client.block.stream.BlockWorkerClient)1 FileSystemContext (alluxio.client.file.FileSystemContext)1 URIStatus (alluxio.client.file.URIStatus)1 InStreamOptions (alluxio.client.file.options.InStreamOptions)1 OutStreamOptions (alluxio.client.file.options.OutStreamOptions)1 IndexDefinition (alluxio.collections.IndexDefinition)1 IndexedSet (alluxio.collections.IndexedSet)1 Pair (alluxio.collections.Pair)1 AlluxioConfiguration (alluxio.conf.AlluxioConfiguration)1 AlluxioProperties (alluxio.conf.AlluxioProperties)1 InstancedConfiguration (alluxio.conf.InstancedConfiguration)1 PropertyKey (alluxio.conf.PropertyKey)1 ServerConfiguration (alluxio.conf.ServerConfiguration)1