1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements.  See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership.  The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License.  You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 package org.apache.hadoop.yarn.api.records;
20 
21 import org.apache.hadoop.classification.InterfaceAudience.Public;
22 import org.apache.hadoop.classification.InterfaceStability.Evolving;
23 import org.apache.hadoop.classification.InterfaceStability.Stable;
24 import org.apache.hadoop.yarn.api.ApplicationMasterProtocol;
25 import org.apache.hadoop.yarn.util.Records;
26 
27 /**
28  * <p><code>Resource</code> models a set of computer resources in the
29  * cluster.</p>
30  *
31  * <p>Currently it models both <em>memory</em> and <em>CPU</em>.</p>
32  *
33  * <p>The unit for memory is megabytes. CPU is modeled with virtual cores
34  * (vcores), a unit for expressing parallelism. A node's capacity should
35  * be configured with virtual cores equal to its number of physical cores. A
36  * container should be requested with the number of cores it can saturate, i.e.
37  * the average number of threads it expects to have runnable at a time.</p>
38  *
39  * <p>Virtual cores take integer values and thus currently CPU-scheduling is
40  * very coarse.  A complementary axis for CPU requests that represents processing
41  * power will likely be added in the future to enable finer-grained resource
42  * configuration.</p>
43  *
44  * <p>Typically, applications request <code>Resource</code> of suitable
45  * capability to run their component tasks.</p>
46  *
47  * @see ResourceRequest
48  * @see ApplicationMasterProtocol#allocate(org.apache.hadoop.yarn.api.protocolrecords.AllocateRequest)
49  */
50 @Public
51 @Stable
52 public abstract class Resource implements Comparable<Resource> {
53 
54   @Public
55   @Stable
newInstance(int memory, int vCores)56   public static Resource newInstance(int memory, int vCores) {
57     Resource resource = Records.newRecord(Resource.class);
58     resource.setMemory(memory);
59     resource.setVirtualCores(vCores);
60     return resource;
61   }
62 
63   /**
64    * Get <em>memory</em> of the resource.
65    * @return <em>memory</em> of the resource
66    */
67   @Public
68   @Stable
getMemory()69   public abstract int getMemory();
70 
71   /**
72    * Set <em>memory</em> of the resource.
73    * @param memory <em>memory</em> of the resource
74    */
75   @Public
76   @Stable
setMemory(int memory)77   public abstract void setMemory(int memory);
78 
79 
80   /**
81    * Get <em>number of virtual cpu cores</em> of the resource.
82    *
83    * Virtual cores are a unit for expressing CPU parallelism. A node's capacity
84    * should be configured with virtual cores equal to its number of physical cores.
85    * A container should be requested with the number of cores it can saturate, i.e.
86    * the average number of threads it expects to have runnable at a time.
87    *
88    * @return <em>num of virtual cpu cores</em> of the resource
89    */
90   @Public
91   @Evolving
getVirtualCores()92   public abstract int getVirtualCores();
93 
94   /**
95    * Set <em>number of virtual cpu cores</em> of the resource.
96    *
97    * Virtual cores are a unit for expressing CPU parallelism. A node's capacity
98    * should be configured with virtual cores equal to its number of physical cores.
99    * A container should be requested with the number of cores it can saturate, i.e.
100    * the average number of threads it expects to have runnable at a time.
101    *
102    * @param vCores <em>number of virtual cpu cores</em> of the resource
103    */
104   @Public
105   @Evolving
setVirtualCores(int vCores)106   public abstract void setVirtualCores(int vCores);
107 
108   @Override
hashCode()109   public int hashCode() {
110     final int prime = 263167;
111     int result = 3571;
112     result = 939769357 + getMemory(); // prime * result = 939769357 initially
113     result = prime * result + getVirtualCores();
114     return result;
115   }
116 
117   @Override
equals(Object obj)118   public boolean equals(Object obj) {
119     if (this == obj)
120       return true;
121     if (obj == null)
122       return false;
123     if (!(obj instanceof Resource))
124       return false;
125     Resource other = (Resource) obj;
126     if (getMemory() != other.getMemory() ||
127         getVirtualCores() != other.getVirtualCores()) {
128       return false;
129     }
130     return true;
131   }
132 
133   @Override
toString()134   public String toString() {
135     return "<memory:" + getMemory() + ", vCores:" + getVirtualCores() + ">";
136   }
137 }
138