|
@@ -35,7 +35,6 @@ import org.apache.hadoop.yarn.util.resource.ResourceUtils;
|
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
-
|
|
|
@Private
|
|
|
@Unstable
|
|
|
public class ResourcePBImpl extends Resource {
|
|
@@ -50,11 +49,14 @@ public class ResourcePBImpl extends Resource {
|
|
|
static ResourceProto getProto(Resource r) {
|
|
|
final ResourcePBImpl pb;
|
|
|
if (r instanceof ResourcePBImpl) {
|
|
|
- pb = (ResourcePBImpl)r;
|
|
|
+ pb = (ResourcePBImpl) r;
|
|
|
} else {
|
|
|
pb = new ResourcePBImpl();
|
|
|
pb.setMemorySize(r.getMemorySize());
|
|
|
pb.setVirtualCores(r.getVirtualCores());
|
|
|
+ for(ResourceInformation res : r.getResources()) {
|
|
|
+ pb.setResourceInformation(res.getName(), res);
|
|
|
+ }
|
|
|
}
|
|
|
return pb.getProto();
|
|
|
}
|
|
@@ -111,7 +113,7 @@ public class ResourcePBImpl extends Resource {
|
|
|
@Override
|
|
|
public void setMemorySize(long memory) {
|
|
|
maybeInitBuilder();
|
|
|
- getResourceInformation(ResourceInformation.MEMORY_URI).setValue(memory);
|
|
|
+ resources[MEMORY_INDEX].setValue(memory);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -123,7 +125,7 @@ public class ResourcePBImpl extends Resource {
|
|
|
@Override
|
|
|
public void setVirtualCores(int vCores) {
|
|
|
maybeInitBuilder();
|
|
|
- getResourceInformation(ResourceInformation.VCORES_URI).setValue(vCores);
|
|
|
+ resources[VCORES_INDEX].setValue(vCores);
|
|
|
}
|
|
|
|
|
|
private void initResources() {
|
|
@@ -131,31 +133,51 @@ public class ResourcePBImpl extends Resource {
|
|
|
return;
|
|
|
}
|
|
|
ResourceProtoOrBuilder p = viaProto ? proto : builder;
|
|
|
- initResourcesMap();
|
|
|
+ ResourceInformation[] types = ResourceUtils.getResourceTypesArray();
|
|
|
Map<String, Integer> indexMap = ResourceUtils.getResourceTypeIndex();
|
|
|
+ resources = new ResourceInformation[types.length];
|
|
|
+
|
|
|
for (ResourceInformationProto entry : p.getResourceValueMapList()) {
|
|
|
- ResourceTypes type =
|
|
|
- entry.hasType() ? ProtoUtils.convertFromProtoFormat(entry.getType()) :
|
|
|
- ResourceTypes.COUNTABLE;
|
|
|
-
|
|
|
- // When unit not specified in proto, use the default unit.
|
|
|
- String units =
|
|
|
- entry.hasUnits() ? entry.getUnits() : ResourceUtils.getDefaultUnit(
|
|
|
- entry.getKey());
|
|
|
- long value = entry.hasValue() ? entry.getValue() : 0L;
|
|
|
- ResourceInformation ri = ResourceInformation
|
|
|
- .newInstance(entry.getKey(), units, value, type, 0L, Long.MAX_VALUE);
|
|
|
Integer index = indexMap.get(entry.getKey());
|
|
|
if (index == null) {
|
|
|
- LOG.warn("Got unknown resource type: " + ri.getName() + "; skipping");
|
|
|
+ LOG.warn("Got unknown resource type: " + entry.getKey() + "; skipping");
|
|
|
} else {
|
|
|
- resources[index].setResourceType(ri.getResourceType());
|
|
|
- resources[index].setUnits(ri.getUnits());
|
|
|
- resources[index].setValue(value);
|
|
|
+ resources[index] = newDefaultInformation(types[index], entry);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ resources[MEMORY_INDEX] = ResourceInformation
|
|
|
+ .newInstance(ResourceInformation.MEMORY_MB);
|
|
|
+ resources[VCORES_INDEX] = ResourceInformation
|
|
|
+ .newInstance(ResourceInformation.VCORES);
|
|
|
this.setMemorySize(p.getMemory());
|
|
|
this.setVirtualCores(p.getVirtualCores());
|
|
|
+
|
|
|
+ // Update missing resource information on respective index.
|
|
|
+ updateResourceInformationMap(types);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void updateResourceInformationMap(ResourceInformation[] types) {
|
|
|
+ for (int i = 0; i < types.length; i++) {
|
|
|
+ if (resources[i] == null) {
|
|
|
+ resources[i] = ResourceInformation.newInstance(types[i]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static ResourceInformation newDefaultInformation(
|
|
|
+ ResourceInformation resourceInformation, ResourceInformationProto entry) {
|
|
|
+ ResourceInformation ri = new ResourceInformation();
|
|
|
+ ri.setName(resourceInformation.getName());
|
|
|
+ ri.setMinimumAllocation(resourceInformation.getMinimumAllocation());
|
|
|
+ ri.setMaximumAllocation(resourceInformation.getMaximumAllocation());
|
|
|
+ ri.setResourceType(entry.hasType()
|
|
|
+ ? ProtoUtils.convertFromProtoFormat(entry.getType())
|
|
|
+ : ResourceTypes.COUNTABLE);
|
|
|
+ ri.setUnits(
|
|
|
+ entry.hasUnits() ? entry.getUnits() : resourceInformation.getUnits());
|
|
|
+ ri.setValue(entry.hasValue() ? entry.getValue() : 0L);
|
|
|
+ return ri;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -166,10 +188,8 @@ public class ResourcePBImpl extends Resource {
|
|
|
throw new IllegalArgumentException(
|
|
|
"resource and/or resourceInformation cannot be null");
|
|
|
}
|
|
|
- if (!resource.equals(resourceInformation.getName())) {
|
|
|
- resourceInformation.setName(resource);
|
|
|
- }
|
|
|
- ResourceInformation storedResourceInfo = getResourceInformation(resource);
|
|
|
+ ResourceInformation storedResourceInfo = super.getResourceInformation(
|
|
|
+ resource);
|
|
|
ResourceInformation.copy(resourceInformation, storedResourceInfo);
|
|
|
}
|
|
|
|
|
@@ -195,25 +215,9 @@ public class ResourcePBImpl extends Resource {
|
|
|
return super.getResourceValue(resource);
|
|
|
}
|
|
|
|
|
|
- private void initResourcesMap() {
|
|
|
- if (resources == null) {
|
|
|
- ResourceInformation[] types = ResourceUtils.getResourceTypesArray();
|
|
|
- if (types == null) {
|
|
|
- throw new YarnRuntimeException(
|
|
|
- "Got null return value from ResourceUtils.getResourceTypes()");
|
|
|
- }
|
|
|
-
|
|
|
- resources = new ResourceInformation[types.length];
|
|
|
- for (ResourceInformation entry : types) {
|
|
|
- int index = ResourceUtils.getResourceTypeIndex().get(entry.getName());
|
|
|
- resources[index] = ResourceInformation.newInstance(entry);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
synchronized private void mergeLocalToBuilder() {
|
|
|
builder.clearResourceValueMap();
|
|
|
- if(resources != null && resources.length != 0) {
|
|
|
+ if (resources != null && resources.length != 0) {
|
|
|
for (ResourceInformation resInfo : resources) {
|
|
|
ResourceInformationProto.Builder e = ResourceInformationProto
|
|
|
.newBuilder();
|
|
@@ -236,4 +240,4 @@ public class ResourcePBImpl extends Resource {
|
|
|
proto = builder.build();
|
|
|
viaProto = true;
|
|
|
}
|
|
|
-}
|
|
|
+}
|