diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/api/records/Resource.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/api/records/Resource.java index 0d31b6fd7d9..0fd41a2f20b 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/api/records/Resource.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/api/records/Resource.java @@ -23,7 +23,6 @@ import org.apache.hadoop.classification.InterfaceAudience.Public; import org.apache.hadoop.classification.InterfaceStability.Evolving; import org.apache.hadoop.classification.InterfaceStability.Stable; import org.apache.hadoop.yarn.api.ApplicationMasterProtocol; -import org.apache.hadoop.yarn.util.Records; /** @@ -53,22 +52,49 @@ import org.apache.hadoop.yarn.util.Records; @Stable public abstract class Resource implements Comparable { + private static class SimpleResource extends Resource { + private long memory; + private long vcores; + SimpleResource(long memory, long vcores) { + this.memory = memory; + this.vcores = vcores; + } + @Override + public int getMemory() { + return (int)memory; + } + @Override + public void setMemory(int memory) { + this.memory = memory; + } + @Override + public long getMemorySize() { + return memory; + } + @Override + public void setMemorySize(long memory) { + this.memory = memory; + } + @Override + public int getVirtualCores() { + return (int)vcores; + } + @Override + public void setVirtualCores(int vcores) { + this.vcores = vcores; + } + } + @Public @Stable public static Resource newInstance(int memory, int vCores) { - Resource resource = Records.newRecord(Resource.class); - resource.setMemorySize(memory); - resource.setVirtualCores(vCores); - return resource; + return new SimpleResource(memory, vCores); } @Public @Stable public static Resource newInstance(long memory, int vCores) { - Resource resource = Records.newRecord(Resource.class); - resource.setMemorySize(memory); - resource.setVirtualCores(vCores); - return resource; + return new SimpleResource(memory, vCores); } /** @@ -167,6 +193,15 @@ public abstract class Resource implements Comparable { return true; } + @Override + public int compareTo(Resource other) { + long diff = this.getMemorySize() - other.getMemorySize(); + if (diff == 0) { + diff = this.getVirtualCores() - other.getVirtualCores(); + } + return diff == 0 ? 0 : (diff > 0 ? 1 : -1); + } + @Override public String toString() { return ""; diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/protocolrecords/impl/pb/AllocateResponsePBImpl.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/protocolrecords/impl/pb/AllocateResponsePBImpl.java index de3fc4781b9..1455d6a82a6 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/protocolrecords/impl/pb/AllocateResponsePBImpl.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/protocolrecords/impl/pb/AllocateResponsePBImpl.java @@ -753,7 +753,7 @@ public class AllocateResponsePBImpl extends AllocateResponse { } private synchronized ResourceProto convertToProtoFormat(Resource r) { - return ((ResourcePBImpl) r).getProto(); + return ProtoUtils.convertToProtoFormat(r); } private synchronized PreemptionMessagePBImpl convertFromProtoFormat(PreemptionMessageProto p) { diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/protocolrecords/impl/pb/GetNewApplicationResponsePBImpl.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/protocolrecords/impl/pb/GetNewApplicationResponsePBImpl.java index eb8ca2c5968..155e7562904 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/protocolrecords/impl/pb/GetNewApplicationResponsePBImpl.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/protocolrecords/impl/pb/GetNewApplicationResponsePBImpl.java @@ -25,6 +25,7 @@ import org.apache.hadoop.yarn.api.protocolrecords.GetNewApplicationResponse; import org.apache.hadoop.yarn.api.records.ApplicationId; import org.apache.hadoop.yarn.api.records.Resource; import org.apache.hadoop.yarn.api.records.impl.pb.ApplicationIdPBImpl; +import org.apache.hadoop.yarn.api.records.impl.pb.ProtoUtils; import org.apache.hadoop.yarn.api.records.impl.pb.ResourcePBImpl; import org.apache.hadoop.yarn.proto.YarnProtos.ApplicationIdProto; import org.apache.hadoop.yarn.proto.YarnProtos.ResourceProto; @@ -158,13 +159,13 @@ public class GetNewApplicationResponsePBImpl extends GetNewApplicationResponse { private ApplicationIdProto convertToProtoFormat(ApplicationId t) { return ((ApplicationIdPBImpl)t).getProto(); } - + private Resource convertFromProtoFormat(ResourceProto resource) { - return new ResourcePBImpl(resource); + return new ResourcePBImpl(resource); } private ResourceProto convertToProtoFormat(Resource resource) { - return ((ResourcePBImpl)resource).getProto(); + return ProtoUtils.convertToProtoFormat(resource); } -} +} diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/protocolrecords/impl/pb/RegisterApplicationMasterResponsePBImpl.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/protocolrecords/impl/pb/RegisterApplicationMasterResponsePBImpl.java index a95aadfbef8..1a70933a284 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/protocolrecords/impl/pb/RegisterApplicationMasterResponsePBImpl.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/protocolrecords/impl/pb/RegisterApplicationMasterResponsePBImpl.java @@ -438,7 +438,7 @@ public class RegisterApplicationMasterResponsePBImpl extends } private ResourceProto convertToProtoFormat(Resource resource) { - return ((ResourcePBImpl)resource).getProto(); + return ProtoUtils.convertToProtoFormat(resource); } private ContainerPBImpl convertFromProtoFormat(ContainerProto p) { diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/records/impl/pb/ApplicationResourceUsageReportPBImpl.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/records/impl/pb/ApplicationResourceUsageReportPBImpl.java index b6a38e7fc70..1c85e28dca8 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/records/impl/pb/ApplicationResourceUsageReportPBImpl.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/records/impl/pb/ApplicationResourceUsageReportPBImpl.java @@ -79,20 +79,14 @@ extends ApplicationResourceUsageReport { } private void mergeLocalToBuilder() { - if (this.usedResources != null - && !((ResourcePBImpl) this.usedResources).getProto().equals( - builder.getUsedResources())) { + if (this.usedResources != null) { builder.setUsedResources(convertToProtoFormat(this.usedResources)); } - if (this.reservedResources != null - && !((ResourcePBImpl) this.reservedResources).getProto().equals( - builder.getReservedResources())) { + if (this.reservedResources != null) { builder.setReservedResources( convertToProtoFormat(this.reservedResources)); } - if (this.neededResources != null - && !((ResourcePBImpl) this.neededResources).getProto().equals( - builder.getNeededResources())) { + if (this.neededResources != null) { builder.setNeededResources(convertToProtoFormat(this.neededResources)); } } @@ -257,7 +251,7 @@ extends ApplicationResourceUsageReport { } private ResourceProto convertToProtoFormat(Resource t) { - return ((ResourcePBImpl)t).getProto(); + return ProtoUtils.convertToProtoFormat(t); } @Override diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/records/impl/pb/ApplicationSubmissionContextPBImpl.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/records/impl/pb/ApplicationSubmissionContextPBImpl.java index 6e46eb6c76b..e3dbf4055be 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/records/impl/pb/ApplicationSubmissionContextPBImpl.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/records/impl/pb/ApplicationSubmissionContextPBImpl.java @@ -119,9 +119,7 @@ extends ApplicationSubmissionContext { if (this.amContainer != null) { builder.setAmContainerSpec(convertToProtoFormat(this.amContainer)); } - if (this.resource != null && - !((ResourcePBImpl) this.resource).getProto().equals( - builder.getResource())) { + if (this.resource != null) { builder.setResource(convertToProtoFormat(this.resource)); } if (this.applicationTags != null && !this.applicationTags.isEmpty()) { @@ -474,7 +472,7 @@ extends ApplicationSubmissionContext { } private ResourceProto convertToProtoFormat(Resource t) { - return ((ResourcePBImpl)t).getProto(); + return ProtoUtils.convertToProtoFormat(t); } @Override diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/records/impl/pb/ContainerPBImpl.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/records/impl/pb/ContainerPBImpl.java index 6bf653dd00a..b6e22d15c64 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/records/impl/pb/ContainerPBImpl.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/records/impl/pb/ContainerPBImpl.java @@ -93,9 +93,7 @@ public class ContainerPBImpl extends Container { builder.getNodeId())) { builder.setNodeId(convertToProtoFormat(this.nodeId)); } - if (this.resource != null - && !((ResourcePBImpl) this.resource).getProto().equals( - builder.getResource())) { + if (this.resource != null) { builder.setResource(convertToProtoFormat(this.resource)); } if (this.priority != null && @@ -307,7 +305,7 @@ public class ContainerPBImpl extends Container { } private ResourceProto convertToProtoFormat(Resource t) { - return ((ResourcePBImpl)t).getProto(); + return ProtoUtils.convertToProtoFormat(t); } private PriorityPBImpl convertFromProtoFormat(PriorityProto p) { diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/records/impl/pb/ContainerReportPBImpl.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/records/impl/pb/ContainerReportPBImpl.java index 1f0405f7274..5d435da9827 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/records/impl/pb/ContainerReportPBImpl.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/records/impl/pb/ContainerReportPBImpl.java @@ -270,9 +270,7 @@ public class ContainerReportPBImpl extends ContainerReport { && !((NodeIdPBImpl) nodeId).getProto().equals(builder.getNodeId())) { builder.setNodeId(convertToProtoFormat(this.nodeId)); } - if (this.resource != null - && !((ResourcePBImpl) this.resource).getProto().equals( - builder.getResource())) { + if (this.resource != null) { builder.setResource(convertToProtoFormat(this.resource)); } if (this.priority != null @@ -318,7 +316,7 @@ public class ContainerReportPBImpl extends ContainerReport { } private ResourceProto convertToProtoFormat(Resource t) { - return ((ResourcePBImpl) t).getProto(); + return ProtoUtils.convertToProtoFormat(t); } private PriorityPBImpl convertFromProtoFormat(PriorityProto p) { diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/records/impl/pb/ContainerStatusPBImpl.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/records/impl/pb/ContainerStatusPBImpl.java index 219cf02289c..11a3d86fda2 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/records/impl/pb/ContainerStatusPBImpl.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/records/impl/pb/ContainerStatusPBImpl.java @@ -339,7 +339,7 @@ public class ContainerStatusPBImpl extends ContainerStatus { } private ResourceProto convertToProtoFormat(Resource e) { - return ((ResourcePBImpl)e).getProto(); + return ProtoUtils.convertToProtoFormat(e); } private ResourcePBImpl convertFromProtoFormat(ResourceProto p) { diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/records/impl/pb/NodeReportPBImpl.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/records/impl/pb/NodeReportPBImpl.java index 1b88ab099e3..0d205e90da1 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/records/impl/pb/NodeReportPBImpl.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/records/impl/pb/NodeReportPBImpl.java @@ -253,14 +253,10 @@ public class NodeReportPBImpl extends NodeReport { builder.getNodeId())) { builder.setNodeId(convertToProtoFormat(this.nodeId)); } - if (this.used != null - && !((ResourcePBImpl) this.used).getProto().equals( - builder.getUsed())) { + if (this.used != null) { builder.setUsed(convertToProtoFormat(this.used)); } - if (this.capability != null - && !((ResourcePBImpl) this.capability).getProto().equals( - builder.getCapability())) { + if (this.capability != null) { builder.setCapability(convertToProtoFormat(this.capability)); } if (this.labels != null) { @@ -310,7 +306,7 @@ public class NodeReportPBImpl extends NodeReport { } private ResourceProto convertToProtoFormat(Resource r) { - return ((ResourcePBImpl) r).getProto(); + return ProtoUtils.convertToProtoFormat(r); } private ResourceUtilizationPBImpl convertFromProtoFormat( diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/records/impl/pb/ProtoUtils.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/records/impl/pb/ProtoUtils.java index 926c757f428..fa9c43011b3 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/records/impl/pb/ProtoUtils.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/records/impl/pb/ProtoUtils.java @@ -346,7 +346,7 @@ public class ProtoUtils { * Resource */ public static synchronized ResourceProto convertToProtoFormat(Resource r) { - return ((ResourcePBImpl) r).getProto(); + return ResourcePBImpl.getProto(r); } public static Resource convertFromProtoFormat(ResourceProto resource) { diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/records/impl/pb/ReservationRequestPBImpl.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/records/impl/pb/ReservationRequestPBImpl.java index e36b9fb3c30..e5a9433952d 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/records/impl/pb/ReservationRequestPBImpl.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/records/impl/pb/ReservationRequestPBImpl.java @@ -140,7 +140,7 @@ public class ReservationRequestPBImpl extends ReservationRequest { } private ResourceProto convertToProtoFormat(Resource t) { - return ((ResourcePBImpl) t).getProto(); + return ProtoUtils.convertToProtoFormat(t); } @Override diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/records/impl/pb/ResourceAllocationRequestPBImpl.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/records/impl/pb/ResourceAllocationRequestPBImpl.java index 737ae44265c..805c628e92a 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/records/impl/pb/ResourceAllocationRequestPBImpl.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/records/impl/pb/ResourceAllocationRequestPBImpl.java @@ -146,7 +146,7 @@ public class ResourceAllocationRequestPBImpl extends } private ResourceProto convertToProtoFormat(Resource p) { - return ((ResourcePBImpl)p).getProto(); + return ProtoUtils.convertToProtoFormat(p); } private void mergeLocalToBuilder() { diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/records/impl/pb/ResourceOptionPBImpl.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/records/impl/pb/ResourceOptionPBImpl.java index 5a4a44e648a..f8e24ad8276 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/records/impl/pb/ResourceOptionPBImpl.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/records/impl/pb/ResourceOptionPBImpl.java @@ -80,7 +80,7 @@ public class ResourceOptionPBImpl extends ResourceOption { private ResourceProto convertToProtoFormat( Resource resource) { - return ((ResourcePBImpl)resource).getProto(); + return ProtoUtils.convertToProtoFormat(resource); } private ResourcePBImpl convertFromProtoFormat( diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/records/impl/pb/ResourcePBImpl.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/records/impl/pb/ResourcePBImpl.java index 6686696cdbc..34109bec0f6 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/records/impl/pb/ResourcePBImpl.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/records/impl/pb/ResourcePBImpl.java @@ -31,7 +31,20 @@ public class ResourcePBImpl extends Resource { ResourceProto proto = ResourceProto.getDefaultInstance(); ResourceProto.Builder builder = null; boolean viaProto = false; - + + // call via ProtoUtils.convertToProtoFormat(Resource) + static ResourceProto getProto(Resource r) { + final ResourcePBImpl pb; + if (r instanceof ResourcePBImpl) { + pb = (ResourcePBImpl)r; + } else { + pb = new ResourcePBImpl(); + pb.setMemorySize(r.getMemorySize()); + pb.setVirtualCores(r.getVirtualCores()); + } + return pb.getProto(); + } + public ResourcePBImpl() { builder = ResourceProto.newBuilder(); } @@ -89,15 +102,4 @@ public class ResourcePBImpl extends Resource { maybeInitBuilder(); builder.setVirtualCores(vCores); } - - @Override - public int compareTo(Resource other) { - long diff = this.getMemorySize() - other.getMemorySize(); - if (diff == 0) { - diff = this.getVirtualCores() - other.getVirtualCores(); - } - return diff == 0 ? 0 : (diff > 0 ? 1 : -1); - } - - } diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/records/impl/pb/ResourceRequestPBImpl.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/records/impl/pb/ResourceRequestPBImpl.java index 9890296acdd..eba539599a8 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/records/impl/pb/ResourceRequestPBImpl.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/api/records/impl/pb/ResourceRequestPBImpl.java @@ -217,7 +217,7 @@ public class ResourceRequestPBImpl extends ResourceRequest { } private ResourceProto convertToProtoFormat(Resource t) { - return ((ResourcePBImpl)t).getProto(); + return ProtoUtils.convertToProtoFormat(t); } @Override diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/security/ContainerTokenIdentifier.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/security/ContainerTokenIdentifier.java index 4b349986de5..07917ef6216 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/security/ContainerTokenIdentifier.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/security/ContainerTokenIdentifier.java @@ -132,7 +132,7 @@ public class ContainerTokenIdentifier extends TokenIdentifier { builder.setNmHostAddr(hostName); builder.setAppSubmitter(appSubmitter); if (r != null) { - builder.setResource(((ResourcePBImpl)r).getProto()); + builder.setResource(ProtoUtils.convertToProtoFormat(r)); } builder.setExpiryTimeStamp(expiryTimeStamp); builder.setMasterKeyId(masterKeyId); diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/util/resource/Resources.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/util/resource/Resources.java index 68db19490d9..b2bb099c56e 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/util/resource/Resources.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/util/resource/Resources.java @@ -21,7 +21,6 @@ package org.apache.hadoop.yarn.util.resource; import org.apache.hadoop.classification.InterfaceAudience; import org.apache.hadoop.classification.InterfaceStability.Unstable; import org.apache.hadoop.yarn.api.records.Resource; -import org.apache.hadoop.yarn.util.Records; @InterfaceAudience.LimitedPrivate({"YARN", "MapReduce"}) @Unstable @@ -123,10 +122,7 @@ public class Resources { } public static Resource createResource(int memory, int cores) { - Resource resource = Records.newRecord(Resource.class); - resource.setMemorySize(memory); - resource.setVirtualCores(cores); - return resource; + return Resource.newInstance(memory, cores); } public static Resource createResource(long memory) { @@ -134,10 +130,7 @@ public class Resources { } public static Resource createResource(long memory, int cores) { - Resource resource = Records.newRecord(Resource.class); - resource.setMemorySize(memory); - resource.setVirtualCores(cores); - return resource; + return Resource.newInstance(memory, cores); } public static Resource none() { diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-applicationhistoryservice/src/main/java/org/apache/hadoop/yarn/server/applicationhistoryservice/records/impl/pb/ContainerStartDataPBImpl.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-applicationhistoryservice/src/main/java/org/apache/hadoop/yarn/server/applicationhistoryservice/records/impl/pb/ContainerStartDataPBImpl.java index 6d248b2d9e8..7b7e9652e45 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-applicationhistoryservice/src/main/java/org/apache/hadoop/yarn/server/applicationhistoryservice/records/impl/pb/ContainerStartDataPBImpl.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-applicationhistoryservice/src/main/java/org/apache/hadoop/yarn/server/applicationhistoryservice/records/impl/pb/ContainerStartDataPBImpl.java @@ -25,6 +25,7 @@ import org.apache.hadoop.yarn.api.records.Resource; import org.apache.hadoop.yarn.api.records.impl.pb.ContainerIdPBImpl; import org.apache.hadoop.yarn.api.records.impl.pb.NodeIdPBImpl; import org.apache.hadoop.yarn.api.records.impl.pb.PriorityPBImpl; +import org.apache.hadoop.yarn.api.records.impl.pb.ProtoUtils; import org.apache.hadoop.yarn.api.records.impl.pb.ResourcePBImpl; import org.apache.hadoop.yarn.proto.ApplicationHistoryServerProtos.ContainerStartDataProto; import org.apache.hadoop.yarn.proto.ApplicationHistoryServerProtos.ContainerStartDataProtoOrBuilder; @@ -189,9 +190,7 @@ public class ContainerStartDataPBImpl extends ContainerStartData { builder.getContainerId())) { builder.setContainerId(convertToProtoFormat(this.containerId)); } - if (this.resource != null - && !((ResourcePBImpl) this.resource).getProto().equals( - builder.getAllocatedResource())) { + if (this.resource != null) { builder.setAllocatedResource(convertToProtoFormat(this.resource)); } if (this.nodeId != null @@ -232,7 +231,7 @@ public class ContainerStartDataPBImpl extends ContainerStartData { } private ResourceProto convertToProtoFormat(Resource resource) { - return ((ResourcePBImpl) resource).getProto(); + return ProtoUtils.convertToProtoFormat(resource); } private ResourcePBImpl convertFromProtoFormat(ResourceProto resource) { diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-common/src/main/java/org/apache/hadoop/yarn/server/api/protocolrecords/impl/pb/NMContainerStatusPBImpl.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-common/src/main/java/org/apache/hadoop/yarn/server/api/protocolrecords/impl/pb/NMContainerStatusPBImpl.java index 2d1046f97b3..2380391e0ee 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-common/src/main/java/org/apache/hadoop/yarn/server/api/protocolrecords/impl/pb/NMContainerStatusPBImpl.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-common/src/main/java/org/apache/hadoop/yarn/server/api/protocolrecords/impl/pb/NMContainerStatusPBImpl.java @@ -256,9 +256,7 @@ public class NMContainerStatusPBImpl extends NMContainerStatus { builder.setContainerId(convertToProtoFormat(this.containerId)); } - if (this.resource != null - && !((ResourcePBImpl) this.resource).getProto().equals( - builder.getResource())) { + if (this.resource != null) { builder.setResource(convertToProtoFormat(this.resource)); } @@ -295,7 +293,7 @@ public class NMContainerStatusPBImpl extends NMContainerStatus { } private ResourceProto convertToProtoFormat(Resource t) { - return ((ResourcePBImpl) t).getProto(); + return ProtoUtils.convertToProtoFormat(t); } private ContainerStateProto diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-common/src/main/java/org/apache/hadoop/yarn/server/api/protocolrecords/impl/pb/NodeHeartbeatResponsePBImpl.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-common/src/main/java/org/apache/hadoop/yarn/server/api/protocolrecords/impl/pb/NodeHeartbeatResponsePBImpl.java index 37bf5706b03..51c1a786d15 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-common/src/main/java/org/apache/hadoop/yarn/server/api/protocolrecords/impl/pb/NodeHeartbeatResponsePBImpl.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-common/src/main/java/org/apache/hadoop/yarn/server/api/protocolrecords/impl/pb/NodeHeartbeatResponsePBImpl.java @@ -646,7 +646,7 @@ public class NodeHeartbeatResponsePBImpl extends NodeHeartbeatResponse { } private ResourceProto convertToProtoFormat(Resource t) { - return ((ResourcePBImpl)t).getProto(); + return ProtoUtils.convertToProtoFormat(t); } private ApplicationIdPBImpl convertFromProtoFormat(ApplicationIdProto p) { diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-common/src/main/java/org/apache/hadoop/yarn/server/api/protocolrecords/impl/pb/RegisterNodeManagerRequestPBImpl.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-common/src/main/java/org/apache/hadoop/yarn/server/api/protocolrecords/impl/pb/RegisterNodeManagerRequestPBImpl.java index 0291e0b8665..eda06d0dd8a 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-common/src/main/java/org/apache/hadoop/yarn/server/api/protocolrecords/impl/pb/RegisterNodeManagerRequestPBImpl.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-common/src/main/java/org/apache/hadoop/yarn/server/api/protocolrecords/impl/pb/RegisterNodeManagerRequestPBImpl.java @@ -32,6 +32,7 @@ import org.apache.hadoop.yarn.api.records.Resource; import org.apache.hadoop.yarn.api.records.impl.pb.ApplicationIdPBImpl; import org.apache.hadoop.yarn.api.records.impl.pb.NodeIdPBImpl; import org.apache.hadoop.yarn.api.records.impl.pb.NodeLabelPBImpl; +import org.apache.hadoop.yarn.api.records.impl.pb.ProtoUtils; import org.apache.hadoop.yarn.api.records.impl.pb.ResourcePBImpl; import org.apache.hadoop.yarn.proto.YarnProtos.ApplicationIdProto; import org.apache.hadoop.yarn.proto.YarnProtos.NodeIdProto; @@ -387,7 +388,7 @@ public class RegisterNodeManagerRequestPBImpl extends RegisterNodeManagerRequest } private static ResourceProto convertToProtoFormat(Resource t) { - return ((ResourcePBImpl)t).getProto(); + return ProtoUtils.convertToProtoFormat(t); } private static NMContainerStatusPBImpl convertFromProtoFormat( diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-common/src/main/java/org/apache/hadoop/yarn/server/api/protocolrecords/impl/pb/RegisterNodeManagerResponsePBImpl.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-common/src/main/java/org/apache/hadoop/yarn/server/api/protocolrecords/impl/pb/RegisterNodeManagerResponsePBImpl.java index 2121fd1726b..63213090d45 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-common/src/main/java/org/apache/hadoop/yarn/server/api/protocolrecords/impl/pb/RegisterNodeManagerResponsePBImpl.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-common/src/main/java/org/apache/hadoop/yarn/server/api/protocolrecords/impl/pb/RegisterNodeManagerResponsePBImpl.java @@ -20,6 +20,7 @@ package org.apache.hadoop.yarn.server.api.protocolrecords.impl.pb; import org.apache.hadoop.yarn.api.records.Resource; +import org.apache.hadoop.yarn.api.records.impl.pb.ProtoUtils; import org.apache.hadoop.yarn.api.records.impl.pb.ResourcePBImpl; import org.apache.hadoop.yarn.proto.YarnProtos.ResourceProto; import org.apache.hadoop.yarn.proto.YarnServerCommonProtos.MasterKeyProto; @@ -253,7 +254,7 @@ public class RegisterNodeManagerResponsePBImpl } private ResourceProto convertToProtoFormat(Resource t) { - return ((ResourcePBImpl)t).getProto(); + return ProtoUtils.convertToProtoFormat(t); } @Override diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-common/src/test/java/org/apache/hadoop/yarn/server/api/protocolrecords/TestProtocolRecords.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-common/src/test/java/org/apache/hadoop/yarn/server/api/protocolrecords/TestProtocolRecords.java index b9cde2443a9..74f19e5a4b9 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-common/src/test/java/org/apache/hadoop/yarn/server/api/protocolrecords/TestProtocolRecords.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-common/src/test/java/org/apache/hadoop/yarn/server/api/protocolrecords/TestProtocolRecords.java @@ -40,6 +40,9 @@ import org.apache.hadoop.yarn.api.records.NodeId; import org.apache.hadoop.yarn.api.records.Priority; import org.apache.hadoop.yarn.api.records.Resource; import org.apache.hadoop.yarn.api.records.impl.pb.ContainerStatusPBImpl; +import org.apache.hadoop.yarn.api.records.impl.pb.ProtoUtils; +import org.apache.hadoop.yarn.api.records.impl.pb.ResourcePBImpl; +import org.apache.hadoop.yarn.proto.YarnProtos.ResourceProto; import org.apache.hadoop.yarn.server.api.protocolrecords.impl.pb.NMContainerStatusPBImpl; import org.apache.hadoop.yarn.server.api.protocolrecords.impl.pb @@ -55,6 +58,23 @@ import org.junit.Test; public class TestProtocolRecords { + @Test + public void testResource() { + final long mem = 123; + final int vcores = 456; + final Resource r = Resource.newInstance(mem, vcores); + // should be a lightweight SimpleResource which is a private inner class + // so just verify it's not the heavyweight pb impl. + Assert.assertFalse(r instanceof ResourcePBImpl); + Assert.assertEquals(mem, r.getMemorySize()); + Assert.assertEquals(vcores, r.getVirtualCores()); + + ResourceProto proto = ProtoUtils.convertToProtoFormat(r); + Assert.assertEquals(mem, proto.getMemory()); + Assert.assertEquals(vcores, proto.getVirtualCores()); + Assert.assertEquals(r, ProtoUtils.convertFromProtoFormat(proto)); + } + @Test public void testNMContainerStatus() { ApplicationId appId = ApplicationId.newInstance(123456789, 1); diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/java/org/apache/hadoop/yarn/server/nodemanager/recovery/NMLeveldbStateStoreService.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/java/org/apache/hadoop/yarn/server/nodemanager/recovery/NMLeveldbStateStoreService.java index ab23456cd1a..d1b9d65ac8d 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/java/org/apache/hadoop/yarn/server/nodemanager/recovery/NMLeveldbStateStoreService.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/java/org/apache/hadoop/yarn/server/nodemanager/recovery/NMLeveldbStateStoreService.java @@ -44,6 +44,7 @@ import org.apache.hadoop.yarn.api.records.ApplicationAttemptId; import org.apache.hadoop.yarn.api.records.ApplicationId; import org.apache.hadoop.yarn.api.records.ContainerId; import org.apache.hadoop.yarn.api.records.Resource; +import org.apache.hadoop.yarn.api.records.impl.pb.ProtoUtils; import org.apache.hadoop.yarn.api.records.impl.pb.ResourcePBImpl; import org.apache.hadoop.yarn.conf.YarnConfiguration; import org.apache.hadoop.yarn.proto.YarnProtos.ResourceProto; @@ -386,7 +387,7 @@ public class NMLeveldbStateStoreService extends NMStateStoreService { try { // New value will overwrite old values for the same key batch.put(bytes(keyResChng), - ((ResourcePBImpl) capability).getProto().toByteArray()); + ProtoUtils.convertToProtoFormat(capability).toByteArray()); batch.put(bytes(keyVersion), bytes(Integer.toString(containerVersion))); db.write(batch); } finally { diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-tests/src/test/java/org/apache/hadoop/yarn/server/ContainerTokenIdentifierForTest.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-tests/src/test/java/org/apache/hadoop/yarn/server/ContainerTokenIdentifierForTest.java index 7e8004d410e..99b3be5850d 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-tests/src/test/java/org/apache/hadoop/yarn/server/ContainerTokenIdentifierForTest.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-tests/src/test/java/org/apache/hadoop/yarn/server/ContainerTokenIdentifierForTest.java @@ -33,6 +33,7 @@ import org.apache.hadoop.yarn.api.records.Resource; import org.apache.hadoop.yarn.api.records.impl.pb.ContainerIdPBImpl; import org.apache.hadoop.yarn.api.records.impl.pb.LogAggregationContextPBImpl; import org.apache.hadoop.yarn.api.records.impl.pb.PriorityPBImpl; +import org.apache.hadoop.yarn.api.records.impl.pb.ProtoUtils; import org.apache.hadoop.yarn.api.records.impl.pb.ResourcePBImpl; import org.apache.hadoop.yarn.security.ContainerTokenIdentifier; import org.apache.hadoop.yarn.proto.YarnProtos.LogAggregationContextProto; @@ -60,7 +61,7 @@ public class ContainerTokenIdentifierForTest extends ContainerTokenIdentifier { builder.setNmHostAddr(hostName); builder.setAppSubmitter(appSubmitter); if (r != null) { - builder.setResource(((ResourcePBImpl)r).getProto()); + builder.setResource(ProtoUtils.convertToProtoFormat(r)); } builder.setExpiryTimeStamp(expiryTimeStamp); builder.setMasterKeyId(masterKeyId); @@ -91,7 +92,7 @@ public class ContainerTokenIdentifierForTest extends ContainerTokenIdentifier { ResourcePBImpl resource = (ResourcePBImpl)identifier.getResource(); if (resource != null) { - builder.setResource(resource.getProto()); + builder.setResource(ProtoUtils.convertToProtoFormat(resource)); } builder.setExpiryTimeStamp(identifier.getExpiryTimeStamp());