YARN-6679. Reduce Resource instance overhead via non-PBImpl

(Contributed by Daryn Sharp via Daniel Templeton)
This commit is contained in:
Daniel Templeton 2017-06-08 10:08:08 -07:00
parent 5672ae7b37
commit a062374c39
26 changed files with 122 additions and 86 deletions

View File

@ -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<Resource> {
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<Resource> {
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 "<memory:" + getMemorySize() + ", vCores:" + getVirtualCores() + ">";

View File

@ -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) {

View File

@ -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;
@ -164,7 +165,7 @@ public class GetNewApplicationResponsePBImpl extends GetNewApplicationResponse {
}
private ResourceProto convertToProtoFormat(Resource resource) {
return ((ResourcePBImpl)resource).getProto();
return ProtoUtils.convertToProtoFormat(resource);
}
}

View File

@ -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) {

View File

@ -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

View File

@ -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

View File

@ -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) {

View File

@ -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) {

View File

@ -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) {

View File

@ -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(

View File

@ -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) {

View File

@ -140,7 +140,7 @@ public class ReservationRequestPBImpl extends ReservationRequest {
}
private ResourceProto convertToProtoFormat(Resource t) {
return ((ResourcePBImpl) t).getProto();
return ProtoUtils.convertToProtoFormat(t);
}
@Override

View File

@ -146,7 +146,7 @@ public class ResourceAllocationRequestPBImpl extends
}
private ResourceProto convertToProtoFormat(Resource p) {
return ((ResourcePBImpl)p).getProto();
return ProtoUtils.convertToProtoFormat(p);
}
private void mergeLocalToBuilder() {

View File

@ -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(

View File

@ -32,6 +32,19 @@ public class ResourcePBImpl extends Resource {
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);
}
}

View File

@ -217,7 +217,7 @@ public class ResourceRequestPBImpl extends ResourceRequest {
}
private ResourceProto convertToProtoFormat(Resource t) {
return ((ResourcePBImpl)t).getProto();
return ProtoUtils.convertToProtoFormat(t);
}
@Override

View File

@ -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);

View File

@ -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() {

View File

@ -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) {

View File

@ -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

View File

@ -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) {

View File

@ -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(

View File

@ -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

View File

@ -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);

View File

@ -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 {

View File

@ -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());