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