Skip to content
This repository has been archived by the owner on Mar 3, 2023. It is now read-only.

Commit

Permalink
Broke out packing builder and utils into separate packages (#1565)
Browse files Browse the repository at this point in the history
* Add packing packages for builder and utils

* Add packing packages for builder and utils
  • Loading branch information
Bill Graham authored Nov 18, 2016
1 parent 26ecadb commit 16eed48
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 64 deletions.
26 changes: 22 additions & 4 deletions heron/packing/src/java/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ packing_deps_files = [
"//heron/common/src/java:basics-java",
"//heron/spi/src/java:packing-spi-java",
"@com_google_guava_guava//jar",
":builder",
":utils",
]

nullpacking_deps_files = \
heron_java_proto_files() + \
packing_deps_files

roundrobin_deps_files = \
heron_java_proto_files() + \
packing_deps_files + [
Expand All @@ -29,6 +27,26 @@ binpacking_deps_files = \
"//heron/spi/src/java:utils-spi-java",
]

java_library(
name='utils',
srcs = glob(["**/utils/*.java"]),
deps = heron_java_proto_files() + [
"//heron/spi/src/java:common-spi-java",
"//heron/spi/src/java:packing-spi-java",
"//heron/spi/src/java:utils-spi-java",
],
)

java_library(
name='builder',
srcs = glob(["**/packing/builder/*.java","**/packing/ResourceExceededException.java"]),
deps = heron_java_proto_files() + [
"@com_google_guava_guava//jar",
"//heron/spi/src/java:packing-spi-java",
":utils",
],
)

java_library(
name='roundrobin-packing',
srcs = glob(["**/roundrobin/**/*.java","**/packing/*.java"]),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
import java.util.logging.Logger;

import com.twitter.heron.api.generated.TopologyAPI;
import com.twitter.heron.packing.PackingPlanBuilder;
import com.twitter.heron.packing.PackingUtils;
import com.twitter.heron.packing.RamRequirement;
import com.twitter.heron.packing.ResourceExceededException;
import com.twitter.heron.packing.builder.PackingPlanBuilder;
import com.twitter.heron.packing.utils.PackingUtils;
import com.twitter.heron.spi.common.Config;
import com.twitter.heron.spi.common.Context;
import com.twitter.heron.spi.packing.IPacking;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.


package com.twitter.heron.packing;
package com.twitter.heron.packing.builder;

import java.util.HashSet;

import com.google.common.base.Optional;

import com.twitter.heron.packing.utils.PackingUtils;
import com.twitter.heron.spi.packing.PackingPlan;
import com.twitter.heron.spi.packing.Resource;

Expand All @@ -29,9 +28,7 @@
class Container {

private HashSet<PackingPlan.InstancePlan> instances;

private Resource capacity;

private int paddingPercentage;

public HashSet<PackingPlan.InstancePlan> getInstances() {
Expand Down Expand Up @@ -59,50 +56,13 @@ public int getPaddingPercentage() {
this.paddingPercentage = paddingPercentage;
}


/**
* Check whether the container can accommodate a new instance with specific resource requirements
*
* @return true if the container has space otherwise return false
*/
private boolean hasSpace(Resource resource) {
Resource usedResources = this.getTotalUsedResources();
long newRam = usedResources.getRam() + resource.getRam();
double newCpu = usedResources.getCpu() + resource.getCpu();
long newDisk = usedResources.getDisk() + resource.getDisk();
boolean ramOk = PackingUtils.increaseBy(newRam, paddingPercentage) <= this.capacity.getRam();
boolean cpuOk =
Math.round(PackingUtils.increaseBy(newCpu, paddingPercentage)) <= this.capacity.getCpu();
boolean diskOk = PackingUtils.increaseBy(newDisk, paddingPercentage) <= this.capacity.getDisk();
return ramOk && cpuOk && diskOk;
}

/**
* Computes the used resources of the container by taking into account the resources
* allocated for each instance.
*
* @return a Resource object that describes the used cpu, ram and disk in the container.
*/
private Resource getTotalUsedResources() {
long usedRam = 0;
double usedCpuCores = 0;
long usedDisk = 0;
for (PackingPlan.InstancePlan instancePlan : this.instances) {
Resource resource = instancePlan.getResource();
usedRam += resource.getRam();
usedCpuCores += resource.getCpu();
usedDisk += resource.getDisk();
}
return new Resource(usedCpuCores, usedRam, usedDisk);
}

/**
* Update the resources currently used by the container, when a new instance with specific
* resource requirements has been assigned to the container.
*
* @return true if the instance can be added to the container, false otherwise
*/
public boolean add(PackingPlan.InstancePlan instancePlan) {
boolean add(PackingPlan.InstancePlan instancePlan) {
if (this.hasSpace(instancePlan.getResource())) {
this.instances.add(instancePlan);
return true;
Expand All @@ -118,7 +78,7 @@ public boolean add(PackingPlan.InstancePlan instancePlan) {
* @return the corresponding instance plan if the instance is removed the container.
* Return void if an instance is not found
*/
public Optional<PackingPlan.InstancePlan> removeAnyInstanceOfComponent(String component) {
Optional<PackingPlan.InstancePlan> removeAnyInstanceOfComponent(String component) {
Optional<PackingPlan.InstancePlan> instancePlan = getAnyInstanceOfComponent(component);
if (instancePlan.isPresent()) {
PackingPlan.InstancePlan plan = instancePlan.get();
Expand All @@ -141,4 +101,40 @@ private Optional<PackingPlan.InstancePlan> getAnyInstanceOfComponent(String comp
}
return Optional.absent();
}

/**
* Check whether the container can accommodate a new instance with specific resource requirements
*
* @return true if the container has space otherwise return false
*/
private boolean hasSpace(Resource resource) {
Resource usedResources = this.getTotalUsedResources();
long newRam = usedResources.getRam() + resource.getRam();
double newCpu = usedResources.getCpu() + resource.getCpu();
long newDisk = usedResources.getDisk() + resource.getDisk();
boolean ramOk = PackingUtils.increaseBy(newRam, paddingPercentage) <= this.capacity.getRam();
boolean cpuOk =
Math.round(PackingUtils.increaseBy(newCpu, paddingPercentage)) <= this.capacity.getCpu();
boolean diskOk = PackingUtils.increaseBy(newDisk, paddingPercentage) <= this.capacity.getDisk();
return ramOk && cpuOk && diskOk;
}

/**
* Computes the used resources of the container by taking into account the resources
* allocated for each instance.
*
* @return a Resource object that describes the used cpu, ram and disk in the container.
*/
private Resource getTotalUsedResources() {
long usedRam = 0;
double usedCpuCores = 0;
long usedDisk = 0;
for (PackingPlan.InstancePlan instancePlan : this.instances) {
Resource resource = instancePlan.getResource();
usedRam += resource.getRam();
usedCpuCores += resource.getCpu();
usedDisk += resource.getDisk();
}
return new Resource(usedCpuCores, usedRam, usedDisk);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.twitter.heron.packing;
package com.twitter.heron.packing.builder;

import java.util.ArrayList;
import java.util.Collections;
Expand All @@ -26,6 +26,8 @@
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Optional;

import com.twitter.heron.packing.ResourceExceededException;
import com.twitter.heron.packing.utils.PackingUtils;
import com.twitter.heron.spi.packing.InstanceId;
import com.twitter.heron.spi.packing.PackingException;
import com.twitter.heron.spi.packing.PackingPlan;
Expand Down Expand Up @@ -234,8 +236,8 @@ private static Set<PackingPlan.ContainerPlan> buildContainerPlans(
*
* @return sorted array of container plans
*/
public static PackingPlan.ContainerPlan[] sortOnContainerId(
Set<PackingPlan.ContainerPlan> containers) {
@VisibleForTesting
static PackingPlan.ContainerPlan[] sortOnContainerId(Set<PackingPlan.ContainerPlan> containers) {
ArrayList<Integer> containerIds = new ArrayList<>();
PackingPlan.ContainerPlan[] currentContainers =
new PackingPlan.ContainerPlan[containers.size()];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
import java.util.logging.Logger;

import com.twitter.heron.api.generated.TopologyAPI;
import com.twitter.heron.packing.PackingPlanBuilder;
import com.twitter.heron.packing.PackingUtils;
import com.twitter.heron.packing.ResourceExceededException;
import com.twitter.heron.packing.builder.PackingPlanBuilder;
import com.twitter.heron.packing.utils.PackingUtils;
import com.twitter.heron.spi.common.Config;
import com.twitter.heron.spi.common.Context;
import com.twitter.heron.spi.packing.IPacking;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.twitter.heron.packing;
package com.twitter.heron.packing.utils;

import java.util.HashMap;
import java.util.Map;
Expand Down
12 changes: 8 additions & 4 deletions heron/packing/tests/java/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ java_test(
srcs = glob(
["**/roundrobin/ResourceCompliantRRPackingTest.java"]
),
deps = roundrobin_deps_files,
deps = roundrobin_deps_files +
["//heron/packing/src/java:utils"],
size = "small",
)

Expand All @@ -79,7 +80,8 @@ java_test(
srcs = glob(
["**/binpacking/FirstFitDecreasingPackingTest.java"]
),
deps = binpacking_deps_files,
deps = binpacking_deps_files +
["//heron/packing/src/java:utils"],
size = "small",
)

Expand All @@ -88,7 +90,8 @@ java_test(
srcs = glob(
["**/PackingUtilsTest.java"]
),
deps = packing_utils_deps_files,
deps = packing_utils_deps_files +
["//heron/packing/src/java:utils"],
size = "small",
)

Expand All @@ -97,7 +100,8 @@ java_test(
srcs = glob(
["**/PackingPlanBuilderTest.java"]
),
deps = packing_utils_deps_files,
deps = packing_utils_deps_files +
["//heron/packing/src/java:builder"],
size = "small",
)

Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

import com.twitter.heron.api.generated.TopologyAPI;
import com.twitter.heron.packing.AssertPacking;
import com.twitter.heron.packing.PackingUtils;
import com.twitter.heron.packing.utils.PackingUtils;
import com.twitter.heron.spi.common.ClusterDefaults;
import com.twitter.heron.spi.common.Config;
import com.twitter.heron.spi.common.Constants;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.twitter.heron.packing;
package com.twitter.heron.packing.builder;

import java.util.Arrays;
import java.util.HashMap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

import com.twitter.heron.api.generated.TopologyAPI;
import com.twitter.heron.packing.AssertPacking;
import com.twitter.heron.packing.PackingUtils;
import com.twitter.heron.packing.utils.PackingUtils;
import com.twitter.heron.spi.common.ClusterDefaults;
import com.twitter.heron.spi.common.Config;
import com.twitter.heron.spi.common.Constants;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.twitter.heron.packing;
package com.twitter.heron.packing.utils;

import java.util.HashMap;
import java.util.Map;
Expand Down

0 comments on commit 16eed48

Please sign in to comment.