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    
019    package org.apache.hadoop.yarn.api.records;
020    
021    import org.apache.hadoop.classification.InterfaceAudience.Public;
022    import org.apache.hadoop.classification.InterfaceStability.Evolving;
023    import org.apache.hadoop.classification.InterfaceStability.Stable;
024    import org.apache.hadoop.yarn.api.ApplicationMasterProtocol;
025    import org.apache.hadoop.yarn.util.Records;
026    
027    /**
028     * <p><code>Resource</code> models a set of computer resources in the 
029     * cluster.</p>
030     * 
031     * <p>Currrently it only models <em>memory</em>.</p>
032     * 
033     * <p>Typically, applications request <code>Resource</code> of suitable
034     * capability to run their component tasks.</p>
035     * 
036     * @see ResourceRequest
037     * @see ApplicationMasterProtocol#allocate(org.apache.hadoop.yarn.api.protocolrecords.AllocateRequest)
038     */
039    @Public
040    @Stable
041    public abstract class Resource implements Comparable<Resource> {
042    
043      @Public
044      @Stable
045      public static Resource newInstance(int memory, int vCores) {
046        Resource resource = Records.newRecord(Resource.class);
047        resource.setMemory(memory);
048        resource.setVirtualCores(vCores);
049        return resource;
050      }
051    
052      /**
053       * Get <em>memory</em> of the resource.
054       * @return <em>memory</em> of the resource
055       */
056      @Public
057      @Stable
058      public abstract int getMemory();
059      
060      /**
061       * Set <em>memory</em> of the resource.
062       * @param memory <em>memory</em> of the resource
063       */
064      @Public
065      @Stable
066      public abstract void setMemory(int memory);
067    
068    
069      /**
070       * Get <em>number of virtual cpu cores</em> of the resource.
071       * 
072       * We refer to <em>virtual cores</em> to clarify that these represent
073       * <em>normalized</em> cores which may have a m:n relationship w.r.t
074       * physical cores available on the compute nodes. Furthermore, they also 
075       * represent <em>idealized</em> cores since the cluster might be composed
076       * of <em>heterogenous</em> nodes.
077       *   
078       * @return <em>num of virtual cpu cores</em> of the resource
079       */
080      @Public
081      @Evolving
082      public abstract int getVirtualCores();
083      
084      /**
085       * Set <em>number of virtual cpu cores</em> of the resource.
086       * 
087       * We refer to <em>virtual cores</em> to clarify that these represent
088       * <em>normalized</em> cores which may have a m:n relationship w.r.t
089       * physical cores available on the compute nodes. Furthermore, they also 
090       * represent <em>idealized</em> cores since the cluster might be composed
091       * of <em>heterogenous</em> nodes.
092       *   
093       * @param vCores <em>number of virtual cpu cores</em> of the resource
094       */
095      @Public
096      @Evolving
097      public abstract void setVirtualCores(int vCores);
098    
099      @Override
100      public int hashCode() {
101        final int prime = 263167;
102        int result = 3571;
103        result = 939769357 + getMemory(); // prime * result = 939769357 initially
104        result = prime * result + getVirtualCores();
105        return result;
106      }
107    
108      @Override
109      public boolean equals(Object obj) {
110        if (this == obj)
111          return true;
112        if (obj == null)
113          return false;
114        if (!(obj instanceof Resource))
115          return false;
116        Resource other = (Resource) obj;
117        if (getMemory() != other.getMemory() || 
118            getVirtualCores() != other.getVirtualCores()) {
119          return false;
120        }
121        return true;
122      }
123    
124      @Override
125      public String toString() {
126        return "<memory:" + getMemory() + ", vCores:" + getVirtualCores() + ">";
127      }
128    }