<li><ahref="#Experimenting_with_placement_constraints_using_distributed_shell">Experimenting with placement constraints using distributed shell</a></li></ul></li>
<li><ahref="#Differences_between_node_labels.2C_node_attributes_and_allocation_tags">Differences between node labels, node attributes and allocation tags</a></li></ul></li>
<li><ahref="#Specifying_constraints_in_applications">Specifying constraints in applications</a></li></ul></li></ul>
<section>
<h2><aname="Overview"></a>Overview</h2>
<p>YARN allows applications to specify placement constraints in the form of data locality (preference to specific nodes or racks) or (non-overlapping) node labels. This document focuses on more expressive placement constraints in YARN. Such constraints can be crucial for the performance and resilience of applications, especially those that include long-running containers, such as services, machine-learning and streaming workloads.</p>
<p>For example, it may be beneficial to co-locate the allocations of a job on the same rack (<i>affinity</i> constraints) to reduce network costs, spread allocations across machines (<i>anti-affinity</i> constraints) to minimize resource interference, or allow up to a specific number of allocations in a node group (<i>cardinality</i> constraints) to strike a balance between the two. Placement decisions also affect resilience. For example, allocations placed within the same cluster upgrade domain would go offline simultaneously.</p>
<p>The applications can specify constraints without requiring knowledge of the underlying topology of the cluster (e.g., one does not need to specify the specific node or rack where their containers should be placed with constraints) or the other applications deployed. Currently, all constraints are <b>hard</b>, that is, if a constraint for a container cannot be satisfied due to the current cluster condition or conflicting constraints, the container request will remain pending or get rejected.</p>
<p>Note that in this document we use the notion of “allocation” to refer to a unit of resources (e.g., CPU and memory) that gets allocated in a node. In the current implementation of YARN, an allocation corresponds to a single container. However, in case an application uses an allocation to spawn more than one containers, an allocation could correspond to multiple containers.</p></section><section>
<h2><aname="Quick_Guide"></a>Quick Guide</h2>
<p>We first describe how to enable scheduling with placement constraints and then provide examples of how to experiment with this feature using the distributed shell, an application that allows to run a given shell command on a set of containers.</p><section>
<p>To enable placement constraints, the following property has to be set to <code>placement-processor</code> or <code>scheduler</code> in <b>conf/yarn-site.xml</b>:</p>
<tdalign="left"> Specify which handler will be used to process PlacementConstraints. Acceptable values are: <code>placement-processor</code>, <code>scheduler</code>, and <code>disabled</code>. </td>
<tdalign="left"><code>disabled</code></td></tr>
</tbody>
</table>
<p>We now give more details about each of the three placement constraint handlers:</p>
<ul>
<li><code>placement-processor</code>: Using this handler, the placement of containers with constraints is determined as a pre-processing step before the capacity or the fair scheduler is called. Once the placement is decided, the capacity/fair scheduler is invoked to perform the actual allocation. The advantage of this handler is that it supports all constraint types (affinity, anti-affinity, cardinality). Moreover, it considers multiple containers at a time, which allows to satisfy more constraints than a container-at-a-time approach can achieve. As it sits outside the main scheduler, it can be used by both the capacity and fair schedulers. Note that at the moment it does not account for task priorities within an application, given that such priorities might be conflicting with the placement constraints.</li>
<li><code>scheduler</code>: Using this handler, containers with constraints will be placed by the main scheduler (as of now, only the capacity scheduler supports SchedulingRequests). It currently supports anti-affinity constraints (no affinity or cardinality). The advantage of this handler, when compared to the <code>placement-processor</code>, is that it follows the same ordering rules for queues (sorted by utilization, priority), apps (sorted by FIFO/fairness/priority) and tasks within the same app (priority) that are enforced by the existing main scheduler.</li>
<li><code>disabled</code>: Using this handler, if a SchedulingRequest is asked by an application, the corresponding allocate call will be rejected.</li>
</ul>
<p>The <code>placement-processor</code> handler supports a wider range of constraints and can allow more containers to be placed, especially when applications have demanding constraints or the cluster is highly-utilized (due to considering multiple containers at a time). However, if respecting task priority within an application is important for the user and the capacity scheduler is used, then the <code>scheduler</code> handler should be used instead.</p></section><section>
<h3><aname="Experimenting_with_placement_constraints_using_distributed_shell"></a>Experimenting with placement constraints using distributed shell</h3>
<p>Users can experiment with placement constraints by using the distributed shell application through the following command:</p>
<li>When the <code>-placement_spec</code> argument is specified (except NodeAttributeConstraintExpr) in the distributed shell command, the <code>-num-containers</code> argument should not be used. In case <code>-num-containers</code> argument is used in conjunction with <code>-placement-spec</code>, the former is ignored. This is because in PlacementSpec, we determine the number of containers per tag, making the <code>-num-containers</code> redundant and possibly conflicting. Moreover, if <code>-placement_spec</code> is used, all containers will be requested with GUARANTEED execution type.</li>
<li>When the <code>NodeAttributeConstraintExpr</code> is specified, <code>SourceTag(NumContainers)</code> is optional and the value of <code>-num-containers</code> will be considered for the number of containers to request.</li>
</ul>
<p>An example of PlacementSpec is the following:</p>
<li>place 3 containers with tag “zk” (standing for ZooKeeper) with node anti-affinity to each other, i.e., do not place more than one container per node (notice that in this first constraint, the SourceTag and the TargetTag of the constraint coincide);</li>
<li>place 5 containers with tag “hbase” with affinity to a rack on which containers with tag “zk” are running (i.e., an “hbase” container should not be placed at a rack where an “zk” container is running, given that “zk” is the TargetTag of the second constraint);</li>
<li>place 7 containers with tag “spark” in nodes that have at least one, but no more than three, containers with tag “hbase”.</li>
</ul>
<p>Another example below demonstrates a composite form of constraint:</p>
<divclass="source">
<divclass="source">
<pre>zk(5),AND(IN,RACK,hbase:NOTIN,NODE,zk)
</pre></div></div>
<p>The above constraint uses the conjunction operator <code>AND</code> to combine two constraints. The AND constraint is satisfied when both its children constraints are satisfied. The specific PlacementSpec requests to place 5 “zk” containers in a rack where at least one “hbase” container is running, and on a node that no “zk” container is running. Similarly, an <code>OR</code> operator can be used to define a constraint that is satisfied when at least one of its children constraints is satisfied. Note that in case “zk” and “hbase” are containers belonging to different applications (which is most probably the case in real use cases), the allocation tags in the PlacementSpec should include namespaces, as we describe below (see <ahref="#Allocation_tags_namespace">Allocation tags namespace</a>).</p></section></section><section>
<p>Allocation tags are string tags that an application can associate with (groups of) its containers. Tags are used to identify components of applications. For example, an HBase Master allocation can be tagged with “hbase-m”, and Region Servers with “hbase-rs”. Other examples are “latency-critical” to refer to the more general demands of the allocation, or “app_0041” to denote the job ID. Allocation tags play a key role in constraints, as they allow to refer to multiple allocations that share a common tag.</p>
<p>Note that instead of using the <code>ResourceRequest</code> object to define allocation tags, we use the new <code>SchedulingRequest</code> object. This has many similarities with the <code>ResourceRequest</code>, but better separates the sizing of the requested allocations (number and size of allocations, priority, execution type, etc.), and the constraints dictating how these allocations should be placed (resource name, relaxed locality). Applications can still use <code>ResourceRequest</code> objects, but in order to define allocation tags and constraints, they need to use the <code>SchedulingRequest</code> object. Within a single <code>AllocateRequest</code>, an application should use either the <code>ResourceRequest</code> or the <code>SchedulingRequest</code> objects, but not both of them.</p><section>
<p>Allocation tags might refer to containers of the same or different applications, and are used to express intra- or inter-application constraints, respectively. We use allocation tag namespaces in order to specify the scope of applications that an allocation tag can refer to. By coupling an allocation tag with a namespace, we can restrict whether the tag targets containers that belong to the same application, to a certain group of applications, or to any application in the cluster.</p>
<p>We currently support the following namespaces:</p>
<tdalign="left"> The allocation tag refers to containers of the current application (to which the constraint will be applied). This is the default namespace. </td></tr>
<tdalign="left"> The allocation tag refers to containers of applications that are tagged with the specified application tag. </td></tr>
</tbody>
</table>
<p>To attach an allocation tag namespace <code>ns</code> to a target tag <code>targetTag</code>, we use the syntax <code>ns/allocationTag</code> in the PlacementSpec. Note that the default namespace is <code>SELF</code>, which is used for <b>intra-app</b> constraints. The remaining namespace tags are used to specify <b>inter-app</b> constraints. When the namespace is not specified next to a tag, <code>SELF</code> is assumed.</p>
<p>The example constraints used above could be extended with namespaces as follows:</p>
<p>The semantics of these constraints are the following:</p>
<ul>
<li>place 3 containers with tag “zk” (standing for ZooKeeper) to nodes that do not have “zk” containers from other applications running;</li>
<li>place 5 containers with tag “hbase” with affinity to a rack on which containers with tag “zk” (from any application, be it the same or a different one) are running;</li>
<li>place 7 containers with tag “spark” in nodes that have at least one, but no more than three, containers with tag “hbase” belonging to application with ID <code>appID_0023</code>.</li>
</ul></section><section>
<h4><aname="Differences_between_node_labels.2C_node_attributes_and_allocation_tags"></a>Differences between node labels, node attributes and allocation tags</h4>
<p>The difference between allocation tags and node labels or <ahref="NodeAttributes.html">node attributes</a>, is that allocation tags are attached to allocations and not to nodes. When an allocation gets allocated to a node by the scheduler, the set of tags of that allocation are automatically added to the node for the duration of the allocation. Hence, a node inherits the tags of the allocations that are currently allocated to the node. Likewise, a rack inherits the tags of its nodes. Moreover, similar to node labels and unlike node attributes, allocation tags have no value attached to them. As we show below, our constraints can refer to allocation tags, as well as node labels and node attributes.</p></section></section><section>
<p>Applications can use the public API in the <code>PlacementConstraints</code> to construct placement constraint. Before describing the methods for building constraints, we describe the methods of the <code>PlacementTargets</code> class that are used to construct the target expressions that will then be used in constraints:</p>
<tdalign="left"> Constructs a target expression on a node partition. It is satisfied for nodes that belong to one of the <code>nodePartitions</code>. </td></tr>
<tdalign="left"> Constructs a target expression on a node attribute. It is satisfied if the specified node attribute has one of the specified values. </td></tr>
</tbody>
</table>
<p>Note that the <code>nodeAttribute</code> method above is not yet functional, as it requires the ongoing node attributes feature.</p>
<p>The methods of the <code>PlacementConstraints</code> class for building constraints are the following:</p>
<tdalign="left"> 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"))</code>, allows allocations on nodes that belong to a rack that has at least one allocation with tag “hbase-m”. </td></tr>
<tdalign="left"> 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. </td></tr>
<trclass="b">
<tdalign="left"><code>cardinality(String scope, int minCardinality, int maxCardinality, String... allocationTags)</code></td>
<tdalign="left"> 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. </td></tr>
<trclass="a">
<tdalign="left"><code>minCardinality(String scope, int minCardinality, String... allocationTags)</code></td>
<tdalign="left"> Similar to <code>cardinality(String, int, int, String...)</code>, but determines only the minimum cardinality (the maximum cardinality is unbound). </td></tr>
<trclass="b">
<tdalign="left"><code>maxCardinality(String scope, int maxCardinality, String... allocationTags)</code></td>
<tdalign="left"> Similar to <code>cardinality(String, int, int, String...)</code>, but determines only the maximum cardinality (the minimum cardinality is 0). </td></tr>
<trclass="a">
<tdalign="left"><code>targetCardinality(String scope, int minCardinality, int maxCardinality, String... allocationTags)</code></td>
<tdalign="left"> 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"))</code>, requires an allocation to be placed within a rack that has at least 2 and at most 10 other allocations with tag “zk”. </td></tr>
</tbody>
</table>
<p>The <code>PlacementConstraints</code> class also includes method for building compound constraints (AND/OR expressions with multiple constraints). Adding support for compound constraints is work in progress.</p></section><section>
<h3><aname="Specifying_constraints_in_applications"></a>Specifying constraints in applications</h3>
<p>Applications have to specify the containers for which each constraint will be enabled. To this end, applications can provide a mapping from a set of allocation tags (source tags) to a placement constraint. For example, an entry of this mapping could be “hbase”->constraint1, which means that constraint1 will be applied when scheduling each allocation with tag “hbase”.</p>
<p>When using the <code>placement-processor</code> handler (see <ahref="#Enabling_placement_constraints">Enabling placement constraints</a>), this constraint mapping is specified within the <code>RegisterApplicationMasterRequest</code>.</p>
<p>When using the <code>scheduler</code> handler, the constraints can also be added at each <code>SchedulingRequest</code> object. Each such constraint is valid for the tag of that scheduling request. In case constraints are specified both at the <code>RegisterApplicationMasterRequest</code> and the scheduling requests, the latter override the former.</p></section></section>