Search in sources :

Example 16 with GetWorkerOptions

use of alluxio.client.block.policy.options.GetWorkerOptions in project alluxio by Alluxio.

the class SpecificHostPolicyTest method noMatchingHost.

/**
 * Tests that no worker is chosen when the worker specified in the policy is not part of the
 * worker list.
 */
@Test
public void noMatchingHost() {
    SpecificHostPolicy policy = new SpecificHostPolicy("worker3");
    List<BlockWorkerInfo> workerInfoList = new ArrayList<>();
    workerInfoList.add(new BlockWorkerInfo(new WorkerNetAddress().setHost("worker1F").setRpcPort(PORT).setDataPort(PORT).setWebPort(PORT), Constants.GB, 0));
    workerInfoList.add(new BlockWorkerInfo(new WorkerNetAddress().setHost("worker2").setRpcPort(PORT).setDataPort(PORT).setWebPort(PORT), Constants.GB, 0));
    GetWorkerOptions options = GetWorkerOptions.defaults().setBlockWorkerInfos(workerInfoList).setBlockInfo(new BlockInfo().setLength(2 * (long) Constants.GB));
    Assert.assertNull(policy.getWorker(options));
}
Also used : WorkerNetAddress(alluxio.wire.WorkerNetAddress) BlockInfo(alluxio.wire.BlockInfo) BlockWorkerInfo(alluxio.client.block.BlockWorkerInfo) ArrayList(java.util.ArrayList) GetWorkerOptions(alluxio.client.block.policy.options.GetWorkerOptions) Test(org.junit.Test)

Example 17 with GetWorkerOptions

use of alluxio.client.block.policy.options.GetWorkerOptions in project alluxio by Alluxio.

the class AlluxioBlockStore method getDataSourceAndType.

/**
 * Gets the data source and type of data source of a block. This method is primarily responsible
 * for determining the data source and type of data source. It takes a map of failed workers and
 * their most recently failed time and tries to update it when BlockInStream created failed,
 * attempting to avoid reading from a recently failed worker.
 *
 * @param info the info of the block to read
 * @param status the URIStatus associated with the read request
 * @param policy the policy determining the Alluxio worker location
 * @param failedWorkers the map of workers address to most recent failure time
 * @return the data source and type of data source of the block
 */
public Pair<WorkerNetAddress, BlockInStreamSource> getDataSourceAndType(BlockInfo info, URIStatus status, BlockLocationPolicy policy, Map<WorkerNetAddress, Long> failedWorkers) throws IOException {
    List<BlockLocation> locations = info.getLocations();
    List<BlockWorkerInfo> blockWorkerInfo = Collections.EMPTY_LIST;
    // Initial target workers to read the block given the block locations.
    Set<WorkerNetAddress> workerPool;
    // Note that, it is possible that the blocks have been written as UFS blocks
    if (status.isPersisted() || status.getPersistenceState().equals("TO_BE_PERSISTED")) {
        blockWorkerInfo = mContext.getCachedWorkers();
        if (blockWorkerInfo.isEmpty()) {
            throw new UnavailableException(ExceptionMessage.NO_WORKER_AVAILABLE.getMessage());
        }
        workerPool = blockWorkerInfo.stream().map(BlockWorkerInfo::getNetAddress).collect(toSet());
    } else {
        if (locations.isEmpty()) {
            blockWorkerInfo = mContext.getCachedWorkers();
            if (blockWorkerInfo.isEmpty()) {
                throw new UnavailableException(ExceptionMessage.NO_WORKER_AVAILABLE.getMessage());
            }
            throw new UnavailableException(ExceptionMessage.BLOCK_UNAVAILABLE.getMessage(info.getBlockId()));
        }
        workerPool = locations.stream().map(BlockLocation::getWorkerAddress).collect(toSet());
    }
    // Workers to read the block, after considering failed workers.
    Set<WorkerNetAddress> workers = handleFailedWorkers(workerPool, failedWorkers);
    // TODO(calvin, jianjian): Consider containing these two variables in one object
    BlockInStreamSource dataSourceType = null;
    WorkerNetAddress dataSource = null;
    locations = locations.stream().filter(location -> workers.contains(location.getWorkerAddress())).collect(toList());
    // First try to read data from Alluxio
    if (!locations.isEmpty()) {
        // TODO(calvin): Get location via a policy
        List<WorkerNetAddress> tieredLocations = locations.stream().map(location -> location.getWorkerAddress()).collect(toList());
        Collections.shuffle(tieredLocations);
        Optional<Pair<WorkerNetAddress, Boolean>> nearest = BlockLocationUtils.nearest(mTieredIdentity, tieredLocations, mContext.getClusterConf());
        if (nearest.isPresent()) {
            dataSource = nearest.get().getFirst();
            dataSourceType = nearest.get().getSecond() ? mContext.hasProcessLocalWorker() ? BlockInStreamSource.PROCESS_LOCAL : BlockInStreamSource.NODE_LOCAL : BlockInStreamSource.REMOTE;
        }
    }
    // Can't get data from Alluxio, get it from the UFS instead
    if (dataSource == null) {
        dataSourceType = BlockInStreamSource.UFS;
        Preconditions.checkNotNull(policy, PreconditionMessage.UFS_READ_LOCATION_POLICY_UNSPECIFIED);
        blockWorkerInfo = blockWorkerInfo.stream().filter(workerInfo -> workers.contains(workerInfo.getNetAddress())).collect(toList());
        GetWorkerOptions getWorkerOptions = GetWorkerOptions.defaults().setBlockInfo(new BlockInfo().setBlockId(info.getBlockId()).setLength(info.getLength()).setLocations(locations)).setBlockWorkerInfos(blockWorkerInfo);
        dataSource = policy.getWorker(getWorkerOptions);
        if (dataSource != null) {
            if (mContext.hasProcessLocalWorker() && dataSource.equals(mContext.getNodeLocalWorker())) {
                dataSourceType = BlockInStreamSource.PROCESS_LOCAL;
                LOG.debug("Create BlockInStream to read data from UFS through process local worker {}", dataSource);
            } else {
                LOG.debug("Create BlockInStream to read data from UFS through worker {} " + "(client embedded in local worker process: {}," + "client co-located with worker in different processes: {}, " + "local worker address: {})", dataSource, mContext.hasProcessLocalWorker(), mContext.hasNodeLocalWorker(), mContext.hasNodeLocalWorker() ? mContext.getNodeLocalWorker() : "N/A");
            }
        }
    }
    if (dataSource == null) {
        throw new UnavailableException(ExceptionMessage.NO_WORKER_AVAILABLE.getMessage());
    }
    return new Pair<>(dataSource, dataSourceType);
}
Also used : BlockLocationPolicy(alluxio.client.block.policy.BlockLocationPolicy) WorkerNetAddress(alluxio.wire.WorkerNetAddress) LoggerFactory(org.slf4j.LoggerFactory) BlockInfo(alluxio.wire.BlockInfo) TieredIdentity(alluxio.wire.TieredIdentity) HashMap(java.util.HashMap) BlockOutStream(alluxio.client.block.stream.BlockOutStream) ArrayList(java.util.ArrayList) Lists(com.google.common.collect.Lists) CloseableResource(alluxio.resource.CloseableResource) Map(java.util.Map) BlockLocationUtils(alluxio.client.block.util.BlockLocationUtils) DataWriter(alluxio.client.block.stream.DataWriter) PreconditionMessage(alluxio.exception.PreconditionMessage) TieredIdentityFactory(alluxio.network.TieredIdentityFactory) Collectors.toSet(java.util.stream.Collectors.toSet) Logger(org.slf4j.Logger) ImmutableMap(com.google.common.collect.ImmutableMap) 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) IOException(java.io.IOException) ThreadSafe(javax.annotation.concurrent.ThreadSafe) Pair(alluxio.collections.Pair) GetWorkerOptions(alluxio.client.block.policy.options.GetWorkerOptions) BlockLocation(alluxio.wire.BlockLocation) Collectors.toList(java.util.stream.Collectors.toList) URIStatus(alluxio.client.file.URIStatus) List(java.util.List) FileSystemContext(alluxio.client.file.FileSystemContext) BlockInStreamSource(alluxio.client.block.stream.BlockInStream.BlockInStreamSource) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) VisibleForTesting(com.google.common.annotations.VisibleForTesting) WriteType(alluxio.client.WriteType) Collections(java.util.Collections) UnavailableException(alluxio.exception.status.UnavailableException) UnavailableException(alluxio.exception.status.UnavailableException) BlockInStreamSource(alluxio.client.block.stream.BlockInStream.BlockInStreamSource) GetWorkerOptions(alluxio.client.block.policy.options.GetWorkerOptions) BlockLocation(alluxio.wire.BlockLocation) WorkerNetAddress(alluxio.wire.WorkerNetAddress) BlockInfo(alluxio.wire.BlockInfo) Pair(alluxio.collections.Pair)

Example 18 with GetWorkerOptions

use of alluxio.client.block.policy.options.GetWorkerOptions in project alluxio by Alluxio.

the class ClientIOWritePolicy method getWorker.

/**
 * @param options options
 * @return the address of the worker to write to
 */
@Override
@Nullable
public WorkerNetAddress getWorker(GetWorkerOptions options) {
    Map<WorkerNetAddress, BlockWorkerInfo> eligibleWorkers = new HashMap<>();
    for (BlockWorkerInfo info : options.getBlockWorkerInfos()) {
        eligibleWorkers.put(info.getNetAddress(), info);
    }
    if (!mInitialized) {
        mWorkerInfoList = Lists.newArrayList(options.getBlockWorkerInfos());
        // sort by hashcode
        mWorkerInfoList.sort(Comparator.comparing(w -> w.getNetAddress().getHost()));
        // take the first subset
        mWorkerInfoList = mWorkerInfoList.subList(0, Math.min(MAX_WORKERS.get(), mWorkerInfoList.size()));
        if (mWorkerInfoList.size() < MAX_WORKERS.get()) {
            throw new IllegalStateException(String.format("Not enough eligible workers. expected: %d actual: %d", MAX_WORKERS.get(), mWorkerInfoList.size()));
        }
        mIndex = 0;
        mInitialized = true;
    }
    for (int i = 0; i < mWorkerInfoList.size(); i++) {
        WorkerNetAddress candidate = mWorkerInfoList.get(mIndex).getNetAddress();
        mIndex = (mIndex + 1) % mWorkerInfoList.size();
        BlockWorkerInfo workerInfo = eligibleWorkers.get(candidate);
        if (workerInfo != null && workerInfo.getCapacityBytes() >= options.getBlockInfo().getLength()) {
            return candidate;
        }
    }
    return null;
}
Also used : BlockLocationPolicy(alluxio.client.block.policy.BlockLocationPolicy) WorkerNetAddress(alluxio.wire.WorkerNetAddress) MoreObjects(com.google.common.base.MoreObjects) BlockWorkerInfo(alluxio.client.block.BlockWorkerInfo) HashMap(java.util.HashMap) GetWorkerOptions(alluxio.client.block.policy.options.GetWorkerOptions) List(java.util.List) Lists(com.google.common.collect.Lists) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) AlluxioConfiguration(alluxio.conf.AlluxioConfiguration) Objects(com.google.common.base.Objects) Comparator(java.util.Comparator) Nullable(javax.annotation.Nullable) NotThreadSafe(javax.annotation.concurrent.NotThreadSafe) HashMap(java.util.HashMap) WorkerNetAddress(alluxio.wire.WorkerNetAddress) BlockWorkerInfo(alluxio.client.block.BlockWorkerInfo) Nullable(javax.annotation.Nullable)

Aggregations

GetWorkerOptions (alluxio.client.block.policy.options.GetWorkerOptions)18 BlockInfo (alluxio.wire.BlockInfo)16 ArrayList (java.util.ArrayList)16 BlockWorkerInfo (alluxio.client.block.BlockWorkerInfo)15 Test (org.junit.Test)14 WorkerNetAddress (alluxio.wire.WorkerNetAddress)12 BlockLocationPolicy (alluxio.client.block.policy.BlockLocationPolicy)3 Lists (com.google.common.collect.Lists)3 HashMap (java.util.HashMap)3 List (java.util.List)3 AlluxioConfiguration (alluxio.conf.AlluxioConfiguration)2 UnavailableException (alluxio.exception.status.UnavailableException)2 TieredIdentity (alluxio.wire.TieredIdentity)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 MoreObjects (com.google.common.base.MoreObjects)2 Objects (com.google.common.base.Objects)2 Map (java.util.Map)2 Set (java.util.Set)2 WriteType (alluxio.client.WriteType)1 BlockInStream (alluxio.client.block.stream.BlockInStream)1