Example usage for org.apache.hadoop.yarn.util.resource Resources subtract

List of usage examples for org.apache.hadoop.yarn.util.resource Resources subtract

Introduction

In this page you can find the example usage for org.apache.hadoop.yarn.util.resource Resources subtract.

Prototype

public static Resource subtract(Resource lhs, Resource rhs) 

Source Link

Usage

From source file:org.apache.myriad.scheduler.fgs.YarnNodeCapacityManager.java

License:Apache License

/**
 * Checks if any containers were allocated in the current scheduler run and
 * launches the corresponding Mesos tasks. It also updates the node
 * capacity depending on what portion of the consumed offers were actually
 * used.//w  ww .j  a  va  2s .  c  o  m
 */
@VisibleForTesting
protected void handleContainerAllocation(RMNode rmNode) {
    String host = rmNode.getNodeID().getHost();

    ConsumedOffer consumedOffer = offerLifecycleMgr.drainConsumedOffer(host);
    if (consumedOffer == null) {
        LOGGER.debug("No offer consumed for {}", host);
        return;
    }

    Node node = nodeStore.getNode(host);
    Set<RMContainer> containersBeforeSched = node.getContainerSnapshot();
    Set<RMContainer> containersAfterSched = new HashSet<>(node.getNode().getRunningContainers());

    Set<RMContainer> containersAllocatedByMesosOffer = (containersBeforeSched == null) ? containersAfterSched
            : Sets.difference(containersAfterSched, containersBeforeSched);

    if (containersAllocatedByMesosOffer.isEmpty()) {
        LOGGER.debug("No containers allocated using Mesos offers for host: {}", host);
        for (Protos.Offer offer : consumedOffer.getOffers()) {
            offerLifecycleMgr.declineOffer(offer);
        }
        decrementNodeCapacity(rmNode, OfferUtils.getYarnResourcesFromMesosOffers(consumedOffer.getOffers()));
    } else {
        LOGGER.debug("Containers allocated using Mesos offers for host: {} count: {}", host,
                containersAllocatedByMesosOffer.size());

        // Identify the Mesos tasks that need to be launched
        List<Protos.TaskInfo> tasks = Lists.newArrayList();
        Resource resUsed = Resource.newInstance(0, 0);

        for (RMContainer newContainer : containersAllocatedByMesosOffer) {
            tasks.add(getTaskInfoForContainer(newContainer, consumedOffer, node));
            resUsed = Resources.add(resUsed, newContainer.getAllocatedResource());
        }

        // Reduce node capacity to account for unused offers
        Resource resOffered = OfferUtils.getYarnResourcesFromMesosOffers(consumedOffer.getOffers());
        Resource resUnused = Resources.subtract(resOffered, resUsed);
        decrementNodeCapacity(rmNode, resUnused);
        myriadDriver.getDriver().launchTasks(consumedOffer.getOfferIds(), tasks);
    }

    // No need to hold on to the snapshot anymore
    node.removeContainerSnapshot();
}

From source file:org.apache.myriad.scheduler.fgs.YarnNodeCapacityManager.java

License:Apache License

/**
 * Decrements the capacity for the specified RMNode
 * /*from w  w  w.  j  a  va  2s .  c o m*/
 * @param rmNode
 * @param removedCapacity
 */
public void decrementNodeCapacity(RMNode rmNode, Resource removedCapacity) {
    setNodeCapacity(rmNode, Resources.subtract(rmNode.getTotalCapability(), removedCapacity));
}

From source file:org.apache.tez.dag.app.rm.TaskScheduler.java

License:Apache License

synchronized void preemptIfNeeded() {
    Resource freeResources = Resources.subtract(totalResources, allocatedResources);
    LOG.info("Allocated resource memory: " + allocatedResources.getMemory() + " cpu:"
            + allocatedResources.getVirtualCores());
    assert freeResources.getMemory() >= 0;

    CookieContainerRequest highestPriRequest = null;
    for (CookieContainerRequest request : taskRequests.values()) {
        if (highestPriRequest == null) {
            highestPriRequest = request;
        } else if (isHigherPriority(request.getPriority(), highestPriRequest.getPriority())) {
            highestPriRequest = request;
        }//w ww. j a  v  a2  s  .  com
    }
    if (highestPriRequest != null && !fitsIn(highestPriRequest.getCapability(), freeResources)) {
        // highest priority request will not fit in existing free resources
        // free up some more
        // TODO this is subject to error wrt RM resource normalization
        Map.Entry<Object, Container> preemptedEntry = null;
        for (Map.Entry<Object, Container> entry : taskAllocations.entrySet()) {
            if (!isHigherPriority(highestPriRequest.getPriority(), entry.getValue().getPriority())) {
                // higher or same priority
                continue;
            }
            if (preemptedEntry == null || !isHigherPriority(entry.getValue().getPriority(),
                    preemptedEntry.getValue().getPriority())) {
                // keep the lower priority or the one added later
                preemptedEntry = entry;
            }
        }
        if (preemptedEntry != null) {
            // found something to preempt
            LOG.info("Preempting task: " + preemptedEntry.getKey() + " to free resource for request: "
                    + highestPriRequest + " . Current free resources: " + freeResources);
            deallocateContainer(preemptedEntry.getValue().getId());
            // app client will be notified when after container is killed
            // and we get its completed container status
        }
    }
}

From source file:org.apache.tez.dag.app.rm.YarnTaskSchedulerService.java

License:Apache License

@Override
public void onContainersCompleted(List<ContainerStatus> statuses) {
    if (isStopped.get()) {
        return;/*from  w  w w.  j a  v  a 2 s  . c  o  m*/
    }
    Map<Object, ContainerStatus> appContainerStatus = new HashMap<Object, ContainerStatus>(statuses.size());
    synchronized (this) {
        for (ContainerStatus containerStatus : statuses) {
            ContainerId completedId = containerStatus.getContainerId();
            HeldContainer delayedContainer = heldContainers.get(completedId);

            Object task = releasedContainers.remove(completedId);
            if (task != null) {
                if (delayedContainer != null) {
                    LOG.warn("Held container should be null since releasedContainer is not");
                }
                // TODO later we may want to check if exit code matched expectation
                // e.g. successful container should not come back fail exit code after
                // being released
                // completion of a container we had released earlier
                // an allocated container completed. notify app
                LOG.info("Released container completed:" + completedId + " last allocated to task: " + task);
                appContainerStatus.put(task, containerStatus);
                continue;
            }

            // not found in released containers. check currently allocated containers
            // no need to release this container as the RM has already completed it
            task = unAssignContainer(completedId, false);
            if (delayedContainer != null) {
                heldContainers.remove(completedId);
                Resources.subtract(allocatedResources, delayedContainer.getContainer().getResource());
            } else {
                LOG.warn("Held container expected to be not null for a non-AM-released container");
            }
            if (task != null) {
                // completion of a container we have allocated currently
                // an allocated container completed. notify app
                LOG.info("Allocated container completed:" + completedId + " last allocated to task: " + task);
                appContainerStatus.put(task, containerStatus);
                continue;
            }

            // container neither allocated nor released
            LOG.info("Ignoring unknown container: " + containerStatus.getContainerId());
        }
    }

    // upcall to app must be outside locks
    for (Entry<Object, ContainerStatus> entry : appContainerStatus.entrySet()) {
        appClientDelegate.containerCompleted(entry.getKey(), entry.getValue());
    }
}