001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.apache.hadoop.yarn.api.records;
020
021import org.apache.hadoop.classification.InterfaceAudience.Public;
022import org.apache.hadoop.classification.InterfaceStability.Stable;
023import org.apache.hadoop.yarn.api.AMRMProtocol;
024
025/**
026 * <p><code>ResourceRequest</code> represents the request made by an
027 * application to the <code>ResourceManager</code> to obtain various 
028 * <code>Container</code> allocations.</p>
029 * 
030 * <p>It includes:
031 *   <ul>
032 *     <li>{@link Priority} of the request.</li>
033 *     <li>
034 *       The <em>name</em> of the machine or rack on which the allocation is 
035 *       desired. A special value of <em>*</em> signifies that 
036 *       <em>any</em> host/rack is acceptable to the application.
037 *     </li>
038 *     <li>{@link Resource} required for each request.</li>
039 *     <li>
040 *       Number of containers of such specifications which are required 
041 *       by the application.
042 *     </li>
043 *   </ul>
044 * </p>
045 * 
046 * @see Resource
047 * @see AMRMProtocol#allocate(org.apache.hadoop.yarn.api.protocolrecords.AllocateRequest)
048 */
049@Public
050@Stable
051public abstract class ResourceRequest implements Comparable<ResourceRequest> {
052  /**
053   * Get the <code>Priority</code> of the request.
054   * @return <code>Priority</code> of the request
055   */
056  @Public
057  @Stable
058  public abstract Priority getPriority();
059
060  /**
061   * Set the <code>Priority</code> of the request
062   * @param priority <code>Priority</code> of the request
063   */
064  @Public
065  @Stable
066  public abstract void setPriority(Priority priority);
067  
068  /**
069   * Get the <em>host/rack</em> on which the allocation is desired.
070   * 
071   * A special value of <em>*</em> signifies that <em>any</em> host/rack is 
072   * acceptable.
073   * 
074   * @return <em>host/rack</em> on which the allocation is desired
075   */
076  @Public
077  @Stable
078  public abstract String getHostName();
079
080  /**
081   * Set <em>host/rack</em> on which the allocation is desired.
082   * 
083   * A special value of <em>*</em> signifies that <em>any</em> host/rack is 
084   * acceptable.
085   * 
086   * @param hostName <em>host/rack</em> on which the allocation is desired
087   */
088  @Public
089  @Stable
090  public abstract void setHostName(String hostName);
091  
092  /**
093   * Get the <code>Resource</code> capability of the request.
094   * @return <code>Resource</code> capability of the request
095   */
096  @Public
097  @Stable
098  public abstract Resource getCapability();
099  
100  /**
101   * Set the <code>Resource</code> capability of the request
102   * @param capability <code>Resource</code> capability of the request
103   */
104  @Public
105  @Stable
106  public abstract void setCapability(Resource capability);
107
108  /**
109   * Get the number of containers required with the given specifications.
110   * @return number of containers required with the given specifications
111   */
112  @Public
113  @Stable
114  public abstract int getNumContainers();
115  
116  /**
117   * Set the number of containers required with the given specifications
118   * @param numContainers number of containers required with the given 
119   *                      specifications
120   */
121  @Public
122  @Stable
123  public abstract void setNumContainers(int numContainers);
124
125  @Override
126  public int hashCode() {
127    final int prime = 31;
128    int result = 1;
129    Resource capability = getCapability();
130    String hostName = getHostName();
131    Priority priority = getPriority();
132    result =
133        prime * result + ((capability == null) ? 0 : capability.hashCode());
134    result = prime * result + ((hostName == null) ? 0 : hostName.hashCode());
135    result = prime * result + getNumContainers();
136    result = prime * result + ((priority == null) ? 0 : priority.hashCode());
137    return result;
138  }
139
140  @Override
141  public boolean equals(Object obj) {
142    if (this == obj)
143      return true;
144    if (obj == null)
145      return false;
146    if (getClass() != obj.getClass())
147      return false;
148    ResourceRequest other = (ResourceRequest) obj;
149    Resource capability = getCapability();
150    if (capability == null) {
151      if (other.getCapability() != null)
152        return false;
153    } else if (!capability.equals(other.getCapability()))
154      return false;
155    String hostName = getHostName();
156    if (hostName == null) {
157      if (other.getHostName() != null)
158        return false;
159    } else if (!hostName.equals(other.getHostName()))
160      return false;
161    if (getNumContainers() != other.getNumContainers())
162      return false;
163    Priority priority = getPriority();
164    if (priority == null) {
165      if (other.getPriority() != null)
166        return false;
167    } else if (!priority.equals(other.getPriority()))
168      return false;
169    return true;
170  }
171
172  @Override
173  public int compareTo(ResourceRequest other) {
174    int priorityComparison = this.getPriority().compareTo(other.getPriority());
175    if (priorityComparison == 0) {
176      int hostNameComparison =
177          this.getHostName().compareTo(other.getHostName());
178      if (hostNameComparison == 0) {
179        int capabilityComparison =
180            this.getCapability().compareTo(other.getCapability());
181        if (capabilityComparison == 0) {
182          int numContainersComparison =
183              this.getNumContainers() - other.getNumContainers();
184          if (numContainersComparison == 0) {
185            return 0;
186          } else {
187            return numContainersComparison;
188          }
189        } else {
190          return capabilityComparison;
191        }
192      } else {
193        return hostNameComparison;
194      }
195    } else {
196      return priorityComparison;
197    }
198  }
199}