use of com.twitter.heron.spi.packing.InstanceId in project heron by twitter.
the class RoundRobinPacking method pack.
@Override
public PackingPlan pack() {
// Get the instances' round-robin allocation
Map<Integer, List<InstanceId>> roundRobinAllocation = getRoundRobinAllocation();
// Get the ram map for every instance
Map<Integer, Map<InstanceId, ByteAmount>> instancesRamMap = getInstancesRamMapInContainer(roundRobinAllocation);
ByteAmount containerDiskInBytes = getContainerDiskHint(roundRobinAllocation);
double containerCpu = getContainerCpuHint(roundRobinAllocation);
// Construct the PackingPlan
Set<PackingPlan.ContainerPlan> containerPlans = new HashSet<>();
for (int containerId : roundRobinAllocation.keySet()) {
List<InstanceId> instanceList = roundRobinAllocation.get(containerId);
// Calculate the resource required for single instance
Map<InstanceId, PackingPlan.InstancePlan> instancePlanMap = new HashMap<>();
ByteAmount containerRam = DEFAULT_RAM_PADDING_PER_CONTAINER;
for (InstanceId instanceId : instanceList) {
ByteAmount instanceRam = instancesRamMap.get(containerId).get(instanceId);
// Currently not yet support disk or cpu config for different components,
// so just use the default value.
ByteAmount instanceDisk = instanceDiskDefault;
double instanceCpu = instanceCpuDefault;
Resource resource = new Resource(instanceCpu, instanceRam, instanceDisk);
// Insert it into the map
instancePlanMap.put(instanceId, new PackingPlan.InstancePlan(instanceId, resource));
containerRam = containerRam.plus(instanceRam);
}
Resource resource = new Resource(containerCpu, containerRam, containerDiskInBytes);
PackingPlan.ContainerPlan containerPlan = new PackingPlan.ContainerPlan(containerId, new HashSet<>(instancePlanMap.values()), resource);
containerPlans.add(containerPlan);
}
PackingPlan plan = new PackingPlan(topology.getId(), containerPlans);
// Check whether it is a valid PackingPlan
validatePackingPlan(plan);
return plan;
}
use of com.twitter.heron.spi.packing.InstanceId in project heron by twitter.
the class AssertPacking method assertPackingPlan.
public static void assertPackingPlan(String expectedTopologyName, Pair<Integer, InstanceId>[] expectedComponentInstances, PackingPlan plan) {
assertEquals(expectedTopologyName, plan.getId());
assertEquals("Unexpected number of instances: " + plan.getContainers(), expectedComponentInstances.length, plan.getInstanceCount().intValue());
// for every instance on a given container...
Set<Integer> expectedContainerIds = new HashSet<>();
for (Pair<Integer, InstanceId> expectedComponentInstance : expectedComponentInstances) {
// verify the expected container exists
int containerId = expectedComponentInstance.first;
InstanceId instanceId = expectedComponentInstance.second;
assertTrue(String.format("Container with id %s not found", containerId), plan.getContainer(containerId).isPresent());
expectedContainerIds.add(containerId);
// and that the instance exists on it
boolean instanceFound = false;
PackingPlan.ContainerPlan containerPlan = plan.getContainer(containerId).get();
for (PackingPlan.InstancePlan instancePlan : containerPlan.getInstances()) {
if (instancePlan.getTaskId() == instanceId.getTaskId()) {
instanceFound = true;
assertEquals("Wrong componentName for task " + instancePlan.getTaskId(), instanceId.getComponentName(), instancePlan.getComponentName());
assertEquals("Wrong getComponentIndex for task " + instancePlan.getTaskId(), instanceId.getComponentIndex(), instancePlan.getComponentIndex());
break;
}
}
assertTrue(String.format("Container (%s) did not include expected instance with taskId %d", containerPlan, instanceId.getTaskId()), instanceFound);
}
Map<Integer, PackingPlan.InstancePlan> taskIds = new HashMap<>();
Map<String, Set<PackingPlan.InstancePlan>> componentInstances = new HashMap<>();
for (PackingPlan.ContainerPlan containerPlan : plan.getContainers()) {
for (PackingPlan.InstancePlan instancePlan : containerPlan.getInstances()) {
// check for taskId collisions
PackingPlan.InstancePlan collisionInstance = taskIds.get(instancePlan.getTaskId());
assertNull(String.format("Task id collision between instance %s and %s", instancePlan, collisionInstance), collisionInstance);
taskIds.put(instancePlan.getTaskId(), instancePlan);
// check for componentIndex collisions
Set<PackingPlan.InstancePlan> instances = componentInstances.get(instancePlan.getComponentName());
if (instances != null) {
for (PackingPlan.InstancePlan instance : instances) {
assertTrue(String.format("Component index collision between instance %s and %s", instance, instancePlan), instance.getComponentIndex() != instancePlan.getComponentIndex());
}
}
if (componentInstances.get(instancePlan.getComponentName()) == null) {
componentInstances.put(instancePlan.getComponentName(), new HashSet<PackingPlan.InstancePlan>());
}
componentInstances.get(instancePlan.getComponentName()).add(instancePlan);
}
}
assertEquals(expectedContainerIds.size(), plan.getContainers().size());
}
use of com.twitter.heron.spi.packing.InstanceId in project heron by twitter.
the class CommonPackingTests method testScaleDownOneComponentRemoveContainer.
/**
* Test the scenario where scaling down removes instances from containers that are most imbalanced
* (i.e., tending towards homogeneity) first. If there is a tie (e.g. AABB, AB), chooses from the
* container with the fewest instances, to favor ultimately removing containers. If there is
* still a tie, favor removing from higher numbered containers
*/
@Test
public void testScaleDownOneComponentRemoveContainer() throws Exception {
@SuppressWarnings({ "unchecked", "rawtypes" }) Pair<Integer, InstanceId>[] initialComponentInstances = new Pair[] { new Pair<>(1, new InstanceId(A, 1, 0)), new Pair<>(1, new InstanceId(A, 2, 1)), new Pair<>(1, new InstanceId(B, 3, 0)), new Pair<>(3, new InstanceId(B, 4, 1)), new Pair<>(3, new InstanceId(B, 5, 2)), new Pair<>(4, new InstanceId(B, 6, 3)), new Pair<>(4, new InstanceId(B, 7, 4)) };
Map<String, Integer> componentChanges = new HashMap<>();
componentChanges.put(B, -2);
@SuppressWarnings({ "unchecked", "rawtypes" }) Pair<Integer, InstanceId>[] expectedComponentInstances = new Pair[] { new Pair<>(1, new InstanceId(A, 1, 0)), new Pair<>(1, new InstanceId(A, 2, 1)), new Pair<>(1, new InstanceId(B, 3, 0)), new Pair<>(3, new InstanceId(B, 4, 1)), new Pair<>(3, new InstanceId(B, 5, 2)) };
doScaleDownTest(initialComponentInstances, componentChanges, expectedComponentInstances);
}
use of com.twitter.heron.spi.packing.InstanceId in project heron by twitter.
the class PackingPlanBuilderTest method generatePacking.
private static PackingPlan generatePacking(Map<Integer, List<InstanceId>> basePacking) throws RuntimeException {
Resource resource = new Resource(2.0, ByteAmount.fromGigabytes(6), ByteAmount.fromGigabytes(25));
Set<PackingPlan.ContainerPlan> containerPlans = new HashSet<>();
for (int containerId : basePacking.keySet()) {
List<InstanceId> instanceList = basePacking.get(containerId);
Set<PackingPlan.InstancePlan> instancePlans = new HashSet<>();
for (InstanceId instanceId : instanceList) {
String componentName = instanceId.getComponentName();
Resource instanceResource;
switch(componentName) {
case "bolt":
instanceResource = new Resource(1.0, ByteAmount.fromGigabytes(2), ByteAmount.fromGigabytes(10));
break;
case "spout":
instanceResource = new Resource(1.0, ByteAmount.fromGigabytes(3), ByteAmount.fromGigabytes(10));
break;
default:
throw new RuntimeException(String.format("%s is not a valid component name", componentName));
}
instancePlans.add(new PackingPlan.InstancePlan(instanceId, instanceResource));
}
PackingPlan.ContainerPlan containerPlan = new PackingPlan.ContainerPlan(containerId, instancePlans, resource);
containerPlans.add(containerPlan);
}
return new PackingPlan("", containerPlans);
}
use of com.twitter.heron.spi.packing.InstanceId in project heron by twitter.
the class PackingPlanBuilderTest method testRemoveFromPackingPlan.
@Test
public void testRemoveFromPackingPlan() throws ResourceExceededException {
PackingPlan plan = doCreatePackingPlanTest(testContainerInstances);
@SuppressWarnings({ "unchecked", "rawtypes" }) Pair<Integer, String>[] removed = new Pair[] { new Pair<>(1, "componentA"), new Pair<>(3, "componentA") };
PackingPlan updatedPlan = PackingTestHelper.removeFromTestPackingPlan(TOPOLOGY_ID, plan, removed, 0);
@SuppressWarnings({ "unchecked", "rawtypes" }) Pair<Integer, InstanceId>[] expected = new Pair[] { new Pair<>(3, new InstanceId("componentB", 3, 0)) };
AssertPacking.assertPackingPlan(TOPOLOGY_ID, expected, updatedPlan);
}
Aggregations