From a062374c396ba2bddbbe4d3806cc2896293b4e0b Mon Sep 17 00:00:00 2001 From: Daniel Templeton Date: Thu, 8 Jun 2017 10:08:08 -0700 Subject: [PATCH] YARN-6679. Reduce Resource instance overhead via non-PBImpl (Contributed by Daryn Sharp via Daniel Templeton) --- .../hadoop/yarn/api/records/Resource.java | 53 +++++++++++++++---- .../impl/pb/AllocateResponsePBImpl.java | 2 +- .../pb/GetNewApplicationResponsePBImpl.java | 9 ++-- ...gisterApplicationMasterResponsePBImpl.java | 2 +- .../ApplicationResourceUsageReportPBImpl.java | 14 ++--- .../ApplicationSubmissionContextPBImpl.java | 6 +-- .../api/records/impl/pb/ContainerPBImpl.java | 6 +-- .../impl/pb/ContainerReportPBImpl.java | 6 +-- .../impl/pb/ContainerStatusPBImpl.java | 2 +- .../api/records/impl/pb/NodeReportPBImpl.java | 10 ++-- .../yarn/api/records/impl/pb/ProtoUtils.java | 2 +- .../impl/pb/ReservationRequestPBImpl.java | 2 +- .../pb/ResourceAllocationRequestPBImpl.java | 2 +- .../records/impl/pb/ResourceOptionPBImpl.java | 2 +- .../api/records/impl/pb/ResourcePBImpl.java | 26 ++++----- .../impl/pb/ResourceRequestPBImpl.java | 2 +- .../security/ContainerTokenIdentifier.java | 2 +- .../hadoop/yarn/util/resource/Resources.java | 11 +--- .../impl/pb/ContainerStartDataPBImpl.java | 7 ++- .../impl/pb/NMContainerStatusPBImpl.java | 6 +-- .../impl/pb/NodeHeartbeatResponsePBImpl.java | 2 +- .../pb/RegisterNodeManagerRequestPBImpl.java | 3 +- .../pb/RegisterNodeManagerResponsePBImpl.java | 3 +- .../protocolrecords/TestProtocolRecords.java | 20 +++++++ .../recovery/NMLeveldbStateStoreService.java | 3 +- .../ContainerTokenIdentifierForTest.java | 5 +- 26 files changed, 122 insertions(+), 86 deletions(-) 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 0d31b6fd7d..0fd41a2f20 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.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 @@ @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 boolean equals(Object obj) { 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 de3fc4781b..1455d6a82a 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 @@ private synchronized ResourcePBImpl convertFromProtoFormat(ResourceProto p) { } 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 eb8ca2c596..155e756290 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.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 @@ private ApplicationIdPBImpl convertFromProtoFormat(ApplicationIdProto p) { 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 a95aadfbef..1a70933a28 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 @@ private Resource convertFromProtoFormat(ResourceProto resource) { } 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 b6a38e7fc7..1c85e28dca 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 @@ public String toString() { } 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 @@ private ResourcePBImpl convertFromProtoFormat(ResourceProto p) { } 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 6e46eb6c76..e3dbf4055b 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 @@ private void mergeLocalToBuilder() { 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 @@ private ResourcePBImpl convertFromProtoFormat(ResourceProto p) { } 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 6bf653dd00..b6e22d15c6 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 @@ private void mergeLocalToBuilder() { 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 @@ private ResourcePBImpl convertFromProtoFormat(ResourceProto p) { } 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 1f0405f727..5d435da982 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 @@ private void mergeLocalToBuilder() { && !((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 @@ private ResourcePBImpl convertFromProtoFormat(ResourceProto p) { } 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 219cf02289..11a3d86fda 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 @@ private ExecutionTypeProto convertToProtoFormat(ExecutionType e) { } 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 1b88ab099e..0d205e90da 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 @@ private void mergeLocalToBuilder() { 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 @@ private ResourcePBImpl convertFromProtoFormat(ResourceProto p) { } 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 926c757f42..fa9c43011b 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 static ContainerUpdateType convertFromProtoFormat( * 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 e36b9fb3c3..e5a9433952 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 @@ private ResourcePBImpl convertFromProtoFormat(ResourceProto p) { } 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 737ae44265..805c628e92 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 @@ private ResourcePBImpl convertFromProtoFormat(ResourceProto p) { } 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 5a4a44e648..f8e24ad827 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 @@ private void maybeInitBuilder() { 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 6686696cdb..34109bec0f 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 void setVirtualCores(int vCores) { 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 9890296acd..eba539599a 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 @@ private ResourcePBImpl convertFromProtoFormat(ResourceProto p) { } 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 4b349986de..07917ef621 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 ContainerTokenIdentifier(ContainerId containerID, int containerVersion, 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 68db19490d..b2bb099c56 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 @@ 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 static Resource createResource(int memory) { } 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 static Resource createResource(long memory) { } 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 6d248b2d9e..7b7e9652e4 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.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 @@ private void mergeLocalToBuilder() { 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 @@ private ContainerIdProto convertToProtoFormat(ContainerId containerId) { } 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 2d1046f97b..2380391e0e 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 @@ private void mergeLocalToBuilder() { 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 @@ private ResourcePBImpl convertFromProtoFormat(ResourceProto p) { } 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 37bf5706b0..51c1a786d1 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 @@ private ResourcePBImpl convertFromProtoFormat(ResourceProto p) { } 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 0291e0b866..eda06d0dd8 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.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 @@ private static ResourcePBImpl convertFromProtoFormat(ResourceProto p) { } 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 2121fd1726..63213090d4 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 @@ 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 @@ private ResourcePBImpl convertFromProtoFormat(ResourceProto p) { } 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 b9cde2443a..74f19e5a4b 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.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 @@ 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 ab23456cd1..d1b9d65ac8 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.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 void storeContainerResourceChanged(ContainerId containerId, 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 7e8004d410..99b3be5850 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.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 ContainerTokenIdentifierForTest(ContainerId containerID, 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 ContainerTokenIdentifierForTest(ContainerTokenIdentifier identifier, ResourcePBImpl resource = (ResourcePBImpl)identifier.getResource(); if (resource != null) { - builder.setResource(resource.getProto()); + builder.setResource(ProtoUtils.convertToProtoFormat(resource)); } builder.setExpiryTimeStamp(identifier.getExpiryTimeStamp());