Search in sources :

Example 6 with ResourceExceededException

use of com.twitter.heron.packing.ResourceExceededException in project incubator-heron by apache.

the class Container method assertHasSpace.

/**
 * Check whether the container can accommodate a new instance with specific resource requirements
 */
private void assertHasSpace(Resource resource) throws ResourceExceededException {
    Resource usedResources = this.getTotalUsedResources();
    ByteAmount newRam = usedResources.getRam().plus(resource.getRam()).increaseBy(paddingPercentage);
    double newCpu = Math.round(PackingUtils.increaseBy(usedResources.getCpu() + resource.getCpu(), paddingPercentage));
    ByteAmount newDisk = usedResources.getDisk().plus(resource.getDisk()).increaseBy(paddingPercentage);
    if (newRam.greaterThan(this.capacity.getRam())) {
        throw new ResourceExceededException(String.format("Adding %s bytes of ram to existing %s " + "bytes with %d percent padding would exceed capacity %s", resource.getRam(), usedResources.getRam(), paddingPercentage, this.capacity.getRam()));
    }
    if (newCpu > this.capacity.getCpu()) {
        throw new ResourceExceededException(String.format("Adding %s cores to existing %s " + "cores with %d percent padding would exceed capacity %s", resource.getCpu(), usedResources.getCpu(), paddingPercentage, this.capacity.getCpu()));
    }
    if (newDisk.greaterThan(this.capacity.getDisk())) {
        throw new ResourceExceededException(String.format("Adding %s bytes of disk to existing %s " + "bytes with %s percent padding would exceed capacity %s", resource.getDisk(), usedResources.getDisk(), paddingPercentage, this.capacity.getDisk()));
    }
}
Also used : ByteAmount(com.twitter.heron.common.basics.ByteAmount) ResourceExceededException(com.twitter.heron.packing.ResourceExceededException) Resource(com.twitter.heron.spi.packing.Resource)

Example 7 with ResourceExceededException

use of com.twitter.heron.packing.ResourceExceededException in project incubator-heron by apache.

the class PackingPlanBuilder method getContainers.

/**
 * Generates the containers that correspond to the current packing plan
 * along with their associated instances.
 *
 * @return Map of containers for the current packing plan, keyed by containerId
 */
@VisibleForTesting
static Map<Integer, Container> getContainers(PackingPlan currentPackingPlan, int paddingPercentage, Map<String, TreeSet<Integer>> componentIndexes, TreeSet<Integer> taskIds) throws ResourceExceededException {
    Map<Integer, Container> containers = new HashMap<>();
    Resource capacity = currentPackingPlan.getMaxContainerResources();
    for (PackingPlan.ContainerPlan currentContainerPlan : currentPackingPlan.getContainers()) {
        Container container = new Container(currentContainerPlan.getId(), capacity, paddingPercentage);
        for (PackingPlan.InstancePlan instancePlan : currentContainerPlan.getInstances()) {
            try {
                addToContainer(container, instancePlan, componentIndexes, taskIds);
            } catch (ResourceExceededException e) {
                throw new ResourceExceededException(String.format("Insufficient container resources to add instancePlan %s to container %s", instancePlan, container), e);
            }
        }
        containers.put(currentContainerPlan.getId(), container);
    }
    return containers;
}
Also used : HashMap(java.util.HashMap) ResourceExceededException(com.twitter.heron.packing.ResourceExceededException) PackingPlan(com.twitter.heron.spi.packing.PackingPlan) Resource(com.twitter.heron.spi.packing.Resource) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 8 with ResourceExceededException

use of com.twitter.heron.packing.ResourceExceededException in project incubator-heron by apache.

the class PackingPlanBuilder method addInstance.

// adds an instance to a container with id containerId. If that container does not exist, it will
// be lazily initialized, which could result in more containers than those requested using the
// updateNumContainers method
public PackingPlanBuilder addInstance(Integer containerId, String componentName) throws ResourceExceededException {
    initContainer(containerId);
    Integer taskId = taskIds.isEmpty() ? 1 : taskIds.last() + 1;
    Integer componentIndex = componentIndexes.get(componentName) != null ? componentIndexes.get(componentName).last() + 1 : 0;
    InstanceId instanceId = new InstanceId(componentName, taskId, componentIndex);
    Resource instanceResource = PackingUtils.getResourceRequirement(componentName, this.componentRamMap, this.defaultInstanceResource, this.maxContainerResource, this.requestedContainerPadding);
    try {
        addToContainer(containers.get(containerId), new PackingPlan.InstancePlan(instanceId, instanceResource), this.componentIndexes, this.taskIds);
    } catch (ResourceExceededException e) {
        throw new ResourceExceededException(String.format("Insufficient container resources to add instance %s with resources %s to container %d.", instanceId, instanceResource, containerId), e);
    }
    LOG.finest(String.format("Added to container %d instance %s", containerId, instanceId));
    return this;
}
Also used : InstanceId(com.twitter.heron.spi.packing.InstanceId) ResourceExceededException(com.twitter.heron.packing.ResourceExceededException) PackingPlan(com.twitter.heron.spi.packing.PackingPlan) Resource(com.twitter.heron.spi.packing.Resource)

Example 9 with ResourceExceededException

use of com.twitter.heron.packing.ResourceExceededException in project incubator-heron by apache.

the class PackingPlanBuilder method initContainers.

private void initContainers() {
    assertResourceSettings();
    Map<Integer, Container> newContainerMap = this.containers;
    HashMap<String, TreeSet<Integer>> newComponentIndexes = this.componentIndexes;
    TreeSet<Integer> newTaskIds = this.taskIds;
    if (newComponentIndexes == null) {
        newComponentIndexes = new HashMap<>();
    }
    if (newTaskIds == null) {
        newTaskIds = new TreeSet<>();
    }
    // if this is the first time called, initialize container map with empty or existing containers
    if (newContainerMap == null) {
        if (this.existingPacking == null) {
            newContainerMap = new HashMap<>();
            for (int containerId = 1; containerId <= numContainers; containerId++) {
                newContainerMap.put(containerId, new Container(containerId, this.maxContainerResource, this.requestedContainerPadding));
            }
        } else {
            try {
                newContainerMap = getContainers(this.existingPacking, this.requestedContainerPadding, newComponentIndexes, newTaskIds);
            } catch (ResourceExceededException e) {
                throw new PackingException("Could not initialize containers using existing packing plan", e);
            }
        }
    }
    if (this.numContainers > newContainerMap.size()) {
        List<Scorer<Container>> scorers = new ArrayList<>();
        scorers.add(new ContainerIdScorer());
        List<Container> sortedContainers = sortContainers(scorers, newContainerMap.values());
        int nextContainerId = sortedContainers.get(sortedContainers.size() - 1).getContainerId() + 1;
        Resource capacity = newContainerMap.get(sortedContainers.get(0).getContainerId()).getCapacity();
        for (int i = 0; i < numContainers - newContainerMap.size(); i++) {
            newContainerMap.put(nextContainerId, new Container(nextContainerId, capacity, this.requestedContainerPadding));
            nextContainerId++;
        }
    }
    this.containers = newContainerMap;
    this.componentIndexes = newComponentIndexes;
    this.taskIds = newTaskIds;
}
Also used : ResourceExceededException(com.twitter.heron.packing.ResourceExceededException) ArrayList(java.util.ArrayList) Resource(com.twitter.heron.spi.packing.Resource) TreeSet(java.util.TreeSet) PackingException(com.twitter.heron.spi.packing.PackingException)

Example 10 with ResourceExceededException

use of com.twitter.heron.packing.ResourceExceededException in project heron by twitter.

the class PackingPlanBuilder method addInstance.

// adds an instance to a container with id containerId. If that container does not exist, it will
// be lazily initialized, which could result in more containers than those requested using the
// updateNumContainers method
public PackingPlanBuilder addInstance(Integer containerId, String componentName) throws ResourceExceededException {
    initContainer(containerId);
    Integer taskId = taskIds.isEmpty() ? 1 : taskIds.last() + 1;
    Integer componentIndex = componentIndexes.get(componentName) != null ? componentIndexes.get(componentName).last() + 1 : 0;
    InstanceId instanceId = new InstanceId(componentName, taskId, componentIndex);
    Resource instanceResource = PackingUtils.getResourceRequirement(componentName, this.componentRamMap, this.defaultInstanceResource, this.maxContainerResource, this.requestedContainerPadding);
    try {
        addToContainer(containers.get(containerId), new PackingPlan.InstancePlan(instanceId, instanceResource), this.componentIndexes, this.taskIds);
    } catch (ResourceExceededException e) {
        throw new ResourceExceededException(String.format("Insufficient container resources to add instance %s with resources %s to container %d.", instanceId, instanceResource, containerId), e);
    }
    LOG.finest(String.format("Added to container %d instance %s", containerId, instanceId));
    return this;
}
Also used : InstanceId(com.twitter.heron.spi.packing.InstanceId) ResourceExceededException(com.twitter.heron.packing.ResourceExceededException) PackingPlan(com.twitter.heron.spi.packing.PackingPlan) Resource(com.twitter.heron.spi.packing.Resource)

Aggregations

ResourceExceededException (com.twitter.heron.packing.ResourceExceededException)12 Resource (com.twitter.heron.spi.packing.Resource)8 PackingPlanBuilder (com.twitter.heron.packing.builder.PackingPlanBuilder)4 PackingPlan (com.twitter.heron.spi.packing.PackingPlan)4 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 ByteAmount (com.twitter.heron.common.basics.ByteAmount)2 InstanceId (com.twitter.heron.spi.packing.InstanceId)2 PackingException (com.twitter.heron.spi.packing.PackingException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 TreeSet (java.util.TreeSet)2