66 * to you under the Apache License, Version 2.0 (the
77 * "License"); you may not use this file except in compliance
88 * with the License. You may obtain a copy of the License at
9- *
10- * http://www.apache.org/licenses/LICENSE-2.0
11- *
9+ * <p>
10+ * http://www.apache.org/licenses/LICENSE-2.0
11+ * <p>
1212 * Unless required by applicable law or agreed to in writing, software
1313 * distributed under the License is distributed on an "AS IS" BASIS,
1414 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
2424import org .apache .hadoop .classification .InterfaceStability .Stable ;
2525import org .apache .hadoop .yarn .api .ApplicationMasterProtocol ;
2626import org .apache .hadoop .yarn .util .Records ;
27- import java .util .List ;
27+
28+ import java .util .Iterator ;
29+ import java .util .Set ;
2830
2931/**
3032 * <p><code>Resource</code> models a set of computer resources in the
3133 * cluster.</p>
32- *
34+ *
3335 * <p>Currently it models both <em>memory</em> and <em>CPU</em>.</p>
34- *
36+ *
3537 * <p>The unit for memory is megabytes. CPU is modeled with virtual cores
3638 * (vcores), a unit for expressing parallelism. A node's capacity should
3739 * be configured with virtual cores equal to its number of physical cores. A
3840 * container should be requested with the number of cores it can saturate, i.e.
3941 * the average number of threads it expects to have runnable at a time.</p>
40- *
42+ *
4143 * <p>Virtual cores take integer values and thus currently CPU-scheduling is
4244 * very coarse. A complementary axis for CPU requests that represents processing
4345 * power will likely be added in the future to enable finer-grained resource
4446 * configuration.</p>
45- *
47+ *
4648 * <p>Typically, applications request <code>Resource</code> of suitable
4749 * capability to run their component tasks.</p>
48- *
50+ *
4951 * @see ResourceRequest
5052 * @see ApplicationMasterProtocol#allocate(org.apache.hadoop.yarn.api.protocolrecords.AllocateRequest)
5153 */
@@ -73,7 +75,7 @@ public static Resource newInstance(long memory, int vCores) {
7375
7476 @ Public
7577 @ Stable
76- public static Resource newInstance (int memory , int vCores , List <FPGASlot > fpgaSlots ) {
78+ public static Resource newInstance (int memory , int vCores , Set <FPGASlot > fpgaSlots ) {
7779 Resource resource = Records .newRecord (Resource .class );
7880 resource .setMemorySize (memory );
7981 resource .setVirtualCores (vCores );
@@ -83,7 +85,7 @@ public static Resource newInstance(int memory, int vCores, List<FPGASlot> fpgaSl
8385
8486 @ Public
8587 @ Stable
86- public static Resource newInstance (long memory , int vCores , List <FPGASlot > fpgaSlots ) {
88+ public static Resource newInstance (long memory , int vCores , Set <FPGASlot > fpgaSlots ) {
8789 Resource resource = Records .newRecord (Resource .class );
8890 resource .setMemorySize (memory );
8991 resource .setVirtualCores (vCores );
@@ -110,7 +112,7 @@ public static Resource newInstance(long memory, int vCores, List<FPGASlot> fpgaS
110112 @ Stable
111113 public long getMemorySize () {
112114 throw new NotImplementedException (
113- "This method is implemented by ResourcePBImpl" );
115+ "This method is implemented by ResourcePBImpl" );
114116 }
115117
116118 /**
@@ -129,32 +131,32 @@ public long getMemorySize() {
129131 @ Stable
130132 public void setMemorySize (long memory ) {
131133 throw new NotImplementedException (
132- "This method is implemented by ResourcePBImpl" );
134+ "This method is implemented by ResourcePBImpl" );
133135 }
134136
135137
136138 /**
137139 * Get <em>number of virtual cpu cores</em> of the resource.
138- *
140+ *
139141 * Virtual cores are a unit for expressing CPU parallelism. A node's capacity
140142 * should be configured with virtual cores equal to its number of physical cores.
141143 * A container should be requested with the number of cores it can saturate, i.e.
142144 * the average number of threads it expects to have runnable at a time.
143- *
145+ *
144146 * @return <em>num of virtual cpu cores</em> of the resource
145147 */
146148 @ Public
147149 @ Evolving
148150 public abstract int getVirtualCores ();
149-
151+
150152 /**
151153 * Set <em>number of virtual cpu cores</em> of the resource.
152- *
154+ *
153155 * Virtual cores are a unit for expressing CPU parallelism. A node's capacity
154156 * should be configured with virtual cores equal to its number of physical cores.
155157 * A container should be requested with the number of cores it can saturate, i.e.
156158 * the average number of threads it expects to have runnable at a time.
157- *
159+ *
158160 * @param vCores <em>number of virtual cpu cores</em> of the resource
159161 */
160162
@@ -164,19 +166,22 @@ public void setMemorySize(long memory) {
164166
165167 @ Public
166168 @ Evolving
167- public abstract List <FPGASlot > getFPGASlots ();
169+ public abstract Set <FPGASlot > getFPGASlots ();
168170
169171 @ Public
170172 @ Evolving
171- public abstract void setFPGASlots (List <FPGASlot > fpgaSlots );
173+ public abstract void setFPGASlots (Set <FPGASlot > fpgaSlots );
172174
173175 @ Override
174176 public int hashCode () {
175177 final int prime = 263167 ;
176178
177179 int result = (int ) (939769357
178- + getMemorySize ()); // prime * result = 939769357 initially
180+ + getMemorySize ()); // prime * result = 939769357 initially
179181 result = prime * result + getVirtualCores ();
182+ for (FPGASlot fpgaSlot : getFPGASlots ()) {
183+ result = prime * result + fpgaSlot .hashCode ();
184+ }
180185 return result ;
181186 }
182187
@@ -190,23 +195,48 @@ public boolean equals(Object obj) {
190195 return false ;
191196 Resource other = (Resource ) obj ;
192197 if (getMemorySize () != other .getMemorySize () ||
193- getVirtualCores () != other .getVirtualCores ()) {
198+ getVirtualCores () != other .getVirtualCores ()) {
194199 return false ;
195200 }
196- return true ;
201+ return this . getFPGASlots (). equals ( other . getFPGASlots ()) ;
197202 }
198203
199204 @ Override
200205 public String toString () {
201206 StringBuilder fpgaInfo = new StringBuilder ();
202207
203- List <FPGASlot > fpgaSlots = getFPGASlots ();
208+ Set <FPGASlot > fpgaSlots = getFPGASlots ();
209+ if (fpgaSlots == null ) {
210+ return "<memory:" + getMemorySize () + ", vCores:" + getVirtualCores () + ">" ;
211+ }
204212 fpgaInfo .append ("\t \t \t \t FPGA accelerator number:" + fpgaSlots .size () + "\n " );
213+ if (fpgaSlots .size () > 0 ) {
214+ fpgaInfo .append ("\t \t \t \t FPGA accelerator details: \n " );
215+ for (FPGASlot fpgaSlot : fpgaSlots ) {
216+ fpgaInfo .append ("\t \t \t \t fpga type:" + fpgaSlot .getFpgaType () + ", slot id:" + fpgaSlot .getSlotId () + ", afu id:" + fpgaSlot .getAfuId () + "\n " );
217+ }
218+ }
219+ return "<memory:" + getMemorySize () + ", vCores:" + getVirtualCores () + ">\n " + fpgaInfo ;
220+ }
205221
206- fpgaInfo .append ("\t \t \t \t FPGA accelerator details: \n " );
207- for (FPGASlot fpgaSlot : fpgaSlots ) {
208- fpgaInfo .append ("\t \t \t \t fpga type:" + fpgaSlot .getFpgaType () + ", socket id:" + fpgaSlot .getSocketId () + ", slot id:" + fpgaSlot .getSlotId () + ", afu id:" + fpgaSlot .getAfuId () + "\n " );
222+ @ Override
223+ public int compareTo (Resource o ) {
224+ Set <FPGASlot > thisFPGA = this .getFPGASlots ();
225+ Set <FPGASlot > otherFPGA = o .getFPGASlots ();
226+ int diff = thisFPGA .size () - otherFPGA .size ();
227+ if (diff == 0 ) {
228+ diff = this .getVirtualCores () - o .getVirtualCores ();
229+ if (diff == 0 ) {
230+ diff = this .getMemory () - o .getMemory ();
231+ if (diff == 0 ) {
232+ Iterator <FPGASlot > it1 = thisFPGA .iterator ();
233+ Iterator <FPGASlot > it2 = otherFPGA .iterator ();
234+ for (; it1 .hasNext ();) {
235+ diff = it1 .next ().compareTo (it2 .next ());
236+ }
237+ }
238+ }
209239 }
210- return "<memory:" + getMemorySize () + ", vCores:" + getVirtualCores () + "> \n " + fpgaInfo ;
240+ return diff ;
211241 }
212242}
0 commit comments