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()));
}
}
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;
}
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;
}
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;
}
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;
}
Aggregations