@Override public boolean equals(Object o) { if (this == o) { return true; } if (!(o instanceof PlacementConstraint)) { return false; } PlacementConstraint that = (PlacementConstraint) o; return getConstraintExpr() != null ? getConstraintExpr().equals(that .getConstraintExpr()) : that.getConstraintExpr() == null; }
@Override public int hashCode() { return getConstraintExpr() != null ? getConstraintExpr().hashCode() : 0; }
public PlacementConstraintProto convert() { return (PlacementConstraintProto) placementConstraint.getConstraintExpr() .accept(this); }
/** * This method performs the transformation of the * {@link #placementConstraint}. * * @return the transformed placement constraint. */ public PlacementConstraint transform() { AbstractConstraint constraintExpr = placementConstraint.getConstraintExpr(); // Visit the constraint tree to perform the transformation. constraintExpr = constraintExpr.accept(this); return new PlacementConstraint(constraintExpr); }
constraints.stream() .filter(placementConstraint -> placementConstraint != null && placementConstraint.getConstraintExpr() != null) .map(PlacementConstraint::getConstraintExpr) .distinct()
entry.getKey().getTag(), entry.getKey().getNumOfAllocations()); LOG.info("Parsed constraint: {}", entry.getValue() .getConstraintExpr().getClass().getSimpleName()); pSpecs.put(entry.getKey().getTag(), new PlacementSpec( entry.getKey().getTag(),
String nodePartition = RMNodeLabelsManager.NO_LABEL; if (placementConstraint != null && placementConstraint.getConstraintExpr() != null) { PlacementConstraint.AbstractConstraint ac = placementConstraint.getConstraintExpr(); if (ac != null && ac instanceof PlacementConstraint.SingleConstraint) { PlacementConstraint.SingleConstraint singleConstraint =
new SingleConstraintTransformer(constraint); constraint = singleTransformer.transform(); AbstractConstraint sConstraintExpr = constraint.getConstraintExpr();
Assert.assertEquals(expectedPc1, actualPc1.getConstraintExpr()); Assert.assertEquals(expectedPc1, actualPc1.getConstraintExpr()); Assert.assertEquals(expectedPc1, actualPc1.getConstraintExpr()); actualPc1 = valueIt.next(); actualPc2 = valueIt.next(); Assert.assertEquals(expectedPc1, actualPc1.getConstraintExpr()); Assert.assertEquals(expectedPc2, actualPc2.getConstraintExpr()); Assert.assertTrue(actualSourceTags.isEmpty()); actualPc1 = result.values().iterator().next(); Assert.assertEquals(expectedPc1, actualPc1.getConstraintExpr());
Assert.assertTrue(constraint.getConstraintExpr() instanceof And); mergedConstraint = (And) constraint.getConstraintExpr(); Assert.assertEquals(1, mergedConstraint.getChildren().size()); Assert.assertEquals(c1, mergedConstraint.getChildren().get(0).build()); Assert.assertTrue(constraint.getConstraintExpr() instanceof And); mergedConstraint = (And) constraint.getConstraintExpr(); Assert.assertTrue(constraint.getConstraintExpr() instanceof And); mergedConstraint = (And) constraint.getConstraintExpr(); constraint = pcm.getMultilevelConstraint(appId1, Sets.newHashSet(sourceTag1), null); Assert.assertTrue(constraint.getConstraintExpr() instanceof And); mergedConstraint = (And) constraint.getConstraintExpr(); Assert.assertTrue(constraint.getConstraintExpr() instanceof And); mergedConstraint = (And) constraint.getConstraintExpr(); constraint = pcm.getMultilevelConstraint(appId1, Sets.newHashSet(sourceTag1), c1); Assert.assertTrue(constraint.getConstraintExpr() instanceof And); mergedConstraint = (And) constraint.getConstraintExpr();
PlacementConstraint specConstraint = specTransformer.transform(); Or specOrExpr = (Or) specConstraint.getConstraintExpr(); List<AbstractConstraint> specChildren = specOrExpr.getChildren(); Assert.assertEquals(3, specChildren.size()); new SingleConstraintTransformer(specConstraint); PlacementConstraint simConstraint = singleTransformer.transform(); Assert.assertTrue(simConstraint.getConstraintExpr() instanceof Or); Or simOrExpr = (Or) specConstraint.getConstraintExpr(); for (AbstractConstraint child : simOrExpr.getChildren()) { Assert.assertTrue(child instanceof SingleConstraint);
@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()); }
@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()); }
PlacementConstraint newConstraint = fromProtoConverter.convert(); AbstractConstraint newConstraintExpr = newConstraint.getConstraintExpr(); Assert.assertTrue(newConstraintExpr instanceof Or); Or newOrExpr = (Or) newConstraintExpr;
@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 testCardinalityConstraintProtoConverter() { AbstractConstraint sConstraintExpr = cardinality(RACK, 3, 10); Assert.assertTrue(sConstraintExpr instanceof SingleConstraint); SingleConstraint single = (SingleConstraint) sConstraintExpr; PlacementConstraint sConstraint = PlacementConstraints.build(sConstraintExpr); // Convert to proto. PlacementConstraintToProtoConverter toProtoConverter = new PlacementConstraintToProtoConverter(sConstraint); PlacementConstraintProto protoConstraint = toProtoConverter.convert(); compareSimpleConstraintToProto(single, protoConstraint); // Convert from proto. PlacementConstraintFromProtoConverter fromProtoConverter = new PlacementConstraintFromProtoConverter(protoConstraint); PlacementConstraint newConstraint = fromProtoConverter.convert(); AbstractConstraint newConstraintExpr = newConstraint.getConstraintExpr(); Assert.assertTrue(newConstraintExpr instanceof SingleConstraint); SingleConstraint newSingle = (SingleConstraint) newConstraintExpr; compareSimpleConstraints(single, newSingle); }
PlacementConstraint tConstraint = specTransformer.transform(); AbstractConstraint tConstraintExpr = tConstraint.getConstraintExpr(); Assert.assertTrue(tConstraintExpr instanceof TargetConstraint); sConstraint = singleTransformer.transform(); sConstraintExpr = sConstraint.getConstraintExpr(); Assert.assertTrue(sConstraintExpr instanceof SingleConstraint);
PlacementConstraint newConstraint = fromProtoConverter.convert(); AbstractConstraint newConstraintExpr = newConstraint.getConstraintExpr(); Assert.assertTrue(newConstraintExpr instanceof SingleConstraint); SingleConstraint newSingle = (SingleConstraint) newConstraintExpr;