private SingleConstraint convert(SimplePlacementConstraintProto proto) { Set<TargetExpression> targets = new HashSet<>(); for (PlacementConstraintTargetProto tp : proto.getTargetExpressionsList()) { targets.add(convert(tp)); } return new SingleConstraint(proto.getScope(), proto.getMinCardinality(), proto.getMaxCardinality(), convertFromProtoFormat(proto.getAttributeOpCode()), targets); }
/** * This constraint generalizes the cardinality and target constraints. * * Consider a set of nodes N that belongs to the scope specified in the * constraint. If the target expressions are satisfied at least minCardinality * times and at most maxCardinality times in the node set N, then the * constraint is satisfied. * * For example, {@code targetCardinality(RACK, 2, 10, allocationTag("zk"))}, * requires an allocation to be placed within a rack that has at least 2 and * at most 10 other allocations with tag "zk". * * @param scope the scope of the constraint * @param minCardinality the minimum number of times the target expressions * have to be satisfied with the given scope * @param maxCardinality the maximum number of times the target expressions * have to be satisfied with the given scope * @param targetExpressions the target expressions * @return the resulting placement constraint */ public static AbstractConstraint targetCardinality(String scope, int minCardinality, int maxCardinality, TargetExpression... targetExpressions) { return new SingleConstraint(scope, minCardinality, maxCardinality, targetExpressions); }
/** * 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)); }
/** * Creates a constraint that requires allocations to be placed on nodes that * satisfy all target expressions within the given scope (e.g., node or rack). * * For example, {@code targetIn(RACK, allocationTag("hbase-m"))}, allows * allocations on nodes that belong to a rack that has at least one tag with * value "hbase-m". * * @param scope the scope within which the target expressions should be * satisfied * @param targetExpressions the expressions that need to be satisfied within * the scope * @return the resulting placement constraint */ public static AbstractConstraint targetIn(String scope, TargetExpression... targetExpressions) { return new SingleConstraint(scope, 1, Integer.MAX_VALUE, targetExpressions); }
/** * Similar to {@link #cardinality(String, int, int, String...)}, but let you * attach a namespace to the given allocation tags. * * @param scope the scope of the constraint * @param namespace the namespace of the allocation tags * @param minCardinality determines the minimum number of allocations within * the scope * @param maxCardinality determines the maximum number of allocations within * the scope * @param allocationTags allocation tags * @return the resulting placement constraint */ public static AbstractConstraint cardinality(String scope, String namespace, int minCardinality, int maxCardinality, String... allocationTags) { return new SingleConstraint(scope, minCardinality, maxCardinality, PlacementTargets.allocationTagWithNamespace(namespace, allocationTags)); }
/** * Creates a constraint that requires allocations to be placed on nodes that * belong to a scope (e.g., node or rack) that satisfy any of the * target expressions based on node attribute op code. * * @param scope the scope within which the target expressions should not be * true * @param opCode Node Attribute code which could be equals, not equals. * @param targetExpressions the expressions that need to not be true within * the scope * @return the resulting placement constraint */ public static AbstractConstraint targetNodeAttribute(String scope, NodeAttributeOpCode opCode, TargetExpression... targetExpressions) { return new SingleConstraint(scope, -1, -1, opCode, targetExpressions); }
@Override public AbstractConstraint visit(TargetConstraint constraint) { AbstractConstraint newConstraint; if (constraint.getOp() == TargetOperator.IN) { newConstraint = new SingleConstraint(constraint.getScope(), 1, Integer.MAX_VALUE, constraint.getTargetExpressions()); } else if (constraint.getOp() == TargetOperator.NOT_IN) { newConstraint = new SingleConstraint(constraint.getScope(), 0, 0, constraint.getTargetExpressions()); } else { throw new YarnRuntimeException( "Encountered unexpected type of constraint target operator: " + constraint.getOp()); } return newConstraint; }
/** * Creates a constraint that requires allocations to be placed on nodes that * belong to a scope (e.g., node or rack) that does not satisfy any of the * target expressions. * * @param scope the scope within which the target expressions should not be * true * @param targetExpressions the expressions that need to not be true within * the scope * @return the resulting placement constraint */ public static AbstractConstraint targetNotIn(String scope, TargetExpression... targetExpressions) { return new SingleConstraint(scope, 0, 0, targetExpressions); }
@Override public AbstractConstraint visit(CardinalityConstraint constraint) { return new SingleConstraint(constraint.getScope(), constraint.getMinCardinality(), constraint.getMaxCardinality(), new TargetExpression(TargetExpression.TargetType.ALLOCATION_TAG, AllocationTagNamespaceType.SELF.toString(), constraint.getAllocationTags())); } }