target = PlacementConstraints.PlacementTargets.allocationTag( constraintEntities .toArray(new String[constraintEntities.size()]));
@Before public void before() { this.pcm = createPCM(); // Build appIDs, constraints, source tags, and constraint map. long ts = System.currentTimeMillis(); appId1 = BuilderUtils.newApplicationId(ts, 123); appId2 = BuilderUtils.newApplicationId(ts, 234); c1 = PlacementConstraints.build(targetIn(NODE, allocationTag("hbase-m"))); c2 = PlacementConstraints.build(targetIn(RACK, allocationTag("hbase-rs"))); c3 = PlacementConstraints .build(targetNotIn(NODE, nodeAttribute("java", "1.8"))); c4 = PlacementConstraints .build(targetCardinality(RACK, 2, 10, allocationTag("zk"))); sourceTag1 = new HashSet<>(Arrays.asList("spark")); sourceTag2 = new HashSet<>(Arrays.asList("zk")); sourceTag3 = new HashSet<>(Arrays.asList("storm")); sourceTag4 = new HashSet<>(Arrays.asList("hbase-m", "hbase-sec")); constraintMap1 = Stream .of(new SimpleEntry<>(sourceTag1, c1), new SimpleEntry<>(sourceTag2, c2)) .collect(Collectors.toMap(SimpleEntry::getKey, SimpleEntry::getValue)); constraintMap2 = Stream.of(new SimpleEntry<>(sourceTag3, c4)) .collect(Collectors.toMap(SimpleEntry::getKey, SimpleEntry::getValue)); }
appId1 = BuilderUtils.newApplicationId(ts, 123); c1 = PlacementConstraints.build(targetIn(NODE, allocationTag("hbase-m"))); c2 = PlacementConstraints.build(targetIn(RACK, allocationTag("hbase-rs"))); c3 = PlacementConstraints .build(targetNotIn(NODE, allocationTag("hbase-m"))); c4 = PlacementConstraints .build(targetNotIn(RACK, allocationTag("hbase-rs"))); c5 = PlacementConstraints .build(and(targetNotIn(NODE, allocationTag("hbase-m")), maxCardinality(NODE, 3, "spark"))); c6 = PlacementConstraints .build(or(targetIn(NODE, allocationTag("hbase-m")), targetIn(NODE, allocationTag("hbase-rs")))); c7 = PlacementConstraints .build(or(targetIn(NODE, allocationTag("hbase-m")), and(targetIn(NODE, allocationTag("hbase-rs")), targetIn(NODE, allocationTag("spark")))));
Collections.singleton("foo"), PlacementConstraints.build( PlacementConstraints.targetNotIn(NODE, allocationTag("foo"))) )); am1.addSchedulingRequest(
Collections.singletonMap(Collections.singleton("foo"), PlacementConstraints.build(PlacementConstraints .targetCardinality(NODE, 0, 3, allocationTag("foo"))))); am1.addSchedulingRequest( Arrays.asList(schedulingRequest(1, 1, 1, 512, "foo"),
@Test public void testCompositeConstraintProtoConverter() { AbstractConstraint constraintExpr = or(targetIn(RACK, allocationTag("spark")), maxCardinality(NODE, 3), targetCardinality(RACK, 2, 10, allocationTag("zk"))); Assert.assertTrue(constraintExpr instanceof Or); PlacementConstraint constraint = PlacementConstraints.build(constraintExpr);
Collections.singletonMap(Collections.singleton("foo"), PlacementConstraints.build( PlacementConstraints.targetNotIn(NODE, allocationTag("foo"))))); am1.addSchedulingRequest( Arrays.asList(schedulingRequest(1, 1, 1, 512, "foo"),
pcMap.put(Collections.singleton("foo"), PlacementConstraints.build( PlacementConstraints.targetNotIn(NODE, allocationTag("foo")))); pcMap.put(Collections.singleton("bar"), PlacementConstraints.build( PlacementConstraints.targetNotIn(NODE, allocationTag("foo")))); MockAM am1 = MockRM.launchAndRegisterAM(app1, rm, nm2, pcMap); am1.addSchedulingRequest(
PlacementConstraints.build(targetNotIn(NODE, allocationTag("bar")))); PlacementConstraints.build(targetIn(NODE, allocationTag("bar")))); .build(targetCardinality(NODE, 0, 1, allocationTag("foo")))); MockAM am1 = MockRM.launchAndRegisterAM(app1, rm, nm2, constraintMap); am1.addSchedulingRequest(
.targetNotIn(PlacementConstraints.NODE, PlacementConstraints.PlacementTargets .allocationTag("mapper", "reducer"), PlacementConstraints.PlacementTargets.nodePartition("")) .build()).resourceSizing( .targetNotIn(PlacementConstraints.NODE, PlacementConstraints.PlacementTargets .allocationTag("mapper", "reducer"), PlacementConstraints.PlacementTargets.nodePartition("x")) .build()).resourceSizing(
Collections.singletonMap(Collections.singleton("foo"), PlacementConstraints.build( PlacementConstraints.targetIn(NODE, allocationTag("bar"))))); am1.addSchedulingRequest( Arrays.asList(schedulingRequest(1, 1, 1, 512, "bar"),
@Test public void testTargetConstraintProtoConverter() { AbstractConstraint sConstraintExpr = targetIn(NODE, allocationTag("hbase-m")); Assert.assertTrue(sConstraintExpr instanceof SingleConstraint); SingleConstraint single = (SingleConstraint) sConstraintExpr;
@Test public void testCompositeConstraint() { AbstractConstraint constraintExpr = or(targetIn(RACK, allocationTag("spark")), maxCardinality(NODE, 3), targetCardinality(RACK, 2, 10, allocationTag("zk"))); Assert.assertTrue(constraintExpr instanceof Or); PlacementConstraint constraint = PlacementConstraints.build(constraintExpr);
@Test public void testTargetConstraint() { AbstractConstraint sConstraintExpr = targetIn(NODE, allocationTag("hbase-m")); Assert.assertTrue(sConstraintExpr instanceof SingleConstraint); PlacementConstraint sConstraint =
@Test public void testCardinalityConstraint() { CardinalityConstraint cardinality = new CardinalityConstraint(RACK, 3, 10, new HashSet<>(Arrays.asList("hb"))); PlacementConstraint cConstraint = PlacementConstraints.build(cardinality); // Transform from specialized CardinalityConstraint to SimpleConstraint SingleConstraintTransformer singleTransformer = new SingleConstraintTransformer(cConstraint); PlacementConstraint sConstraint = singleTransformer.transform(); AbstractConstraint sConstraintExpr = sConstraint.getConstraintExpr(); Assert.assertTrue(sConstraintExpr instanceof SingleConstraint); SingleConstraint single = (SingleConstraint) sConstraintExpr; // Make sure the consistent expression string is consistent // before and after transforming Assert.assertEquals(single.toString(), cardinality.toString()); Assert.assertEquals(cardinality.getScope(), single.getScope()); Assert.assertEquals(cardinality.getMinCardinality(), single.getMinCardinality()); Assert.assertEquals(cardinality.getMaxCardinality(), single.getMaxCardinality()); Assert.assertEquals( new HashSet<>(Arrays.asList(PlacementTargets.allocationTag("hb"))), single.getTargetExpressions()); }
@Test public void testAndConstraint() { AbstractConstraint constraintExpr = and(targetIn(RACK, allocationTag("spark")), maxCardinality(NODE, 3, "spark"), targetCardinality(RACK, 2, 10, allocationTag("zk"))); And andExpr = (And) constraintExpr; Assert.assertEquals(3, andExpr.getChildren().size()); SingleConstraint sConstr = (SingleConstraint) andExpr.getChildren().get(0); TargetExpression tExpr = sConstr.getTargetExpressions().iterator().next(); Assert.assertEquals("spark", tExpr.getTargetValues().iterator().next()); sConstr = (SingleConstraint) andExpr.getChildren().get(1); Assert.assertEquals(0, sConstr.getMinCardinality()); Assert.assertEquals(3, sConstr.getMaxCardinality()); sConstr = (SingleConstraint) andExpr.getChildren().get(2); Assert.assertEquals(2, sConstr.getMinCardinality()); Assert.assertEquals(10, sConstr.getMaxCardinality()); } }
@Test public void testNodeAffinityToTag() { AbstractConstraint constraintExpr = targetIn(NODE, allocationTag("hbase-m")); SingleConstraint sConstraint = (SingleConstraint) constraintExpr; Assert.assertEquals(NODE, sConstraint.getScope()); Assert.assertEquals(1, sConstraint.getMinCardinality()); Assert.assertEquals(Integer.MAX_VALUE, sConstraint.getMaxCardinality()); Assert.assertEquals(1, sConstraint.getTargetExpressions().size()); TargetExpression tExpr = sConstraint.getTargetExpressions().iterator().next(); Assert.assertEquals(AllocationTagNamespaceType.SELF.toString(), tExpr.getTargetKey()); Assert.assertEquals(TargetType.ALLOCATION_TAG, tExpr.getTargetType()); Assert.assertEquals(1, tExpr.getTargetValues().size()); Assert.assertEquals("hbase-m", tExpr.getTargetValues().iterator().next()); PlacementConstraint constraint = PlacementConstraints.build(constraintExpr); Assert.assertNotNull(constraint.getConstraintExpr()); }
/** * Creates a constraint that restricts the number of allocations within a * given scope (e.g., node or rack). * * For example, {@code cardinality(NODE, 3, 10, "zk")} is satisfied on nodes * where there are no less than 3 allocations with tag "zk" and no more than * 10. * * @param scope the scope of the constraint * @param minCardinality determines the minimum number of allocations within * the scope * @param maxCardinality determines the maximum number of allocations within * the scope * @param allocationTags the constraint targets allocations with these tags * @return the resulting placement constraint */ public static AbstractConstraint cardinality(String scope, int minCardinality, int maxCardinality, String... allocationTags) { return new SingleConstraint(scope, minCardinality, maxCardinality, PlacementTargets.allocationTag(allocationTags)); }
public AllocateResponse allocateIntraAppAntiAffinity( String nodePartition, ResourceSizing resourceSizing, Priority priority, long allocationId, String... tags) throws Exception { return this.allocate(null, Arrays.asList(SchedulingRequest.newBuilder().executionType( ExecutionTypeRequest.newInstance(ExecutionType.GUARANTEED)) .allocationRequestId(allocationId).priority(priority) .placementConstraintExpression(PlacementConstraints .targetNotIn(PlacementConstraints.NODE, PlacementConstraints.PlacementTargets .allocationTag(tags), PlacementConstraints.PlacementTargets .nodePartition(nodePartition)).build()) .resourceSizing(resourceSizing).build()), null); }
@Test public void testTargetCardinalityConstraint() { AbstractConstraint constraintExpr = targetCardinality(RACK, 3, 10, allocationTag("zk")); Assert.assertTrue(constraintExpr instanceof SingleConstraint); PlacementConstraint constraint = PlacementConstraints.build(constraintExpr); // Apply transformation. Should be a no-op. SpecializedConstraintTransformer specTransformer = new SpecializedConstraintTransformer(constraint); PlacementConstraint newConstraint = specTransformer.transform(); // The constraint expression should be the same. Assert.assertEquals(constraintExpr, newConstraint.getConstraintExpr()); }