YARN-4647. Make RegisterNodeManagerRequestPBImpl thread-safe. (kasha)

(cherry picked from commit c9a09d6926b258e205a4ff7998ce5a86bf5dbe3b)
This commit is contained in:
Karthik Kambatla 2016-01-29 08:12:54 -08:00
parent 123269c07f
commit 9d1371d390
2 changed files with 42 additions and 35 deletions

View File

@ -55,6 +55,8 @@ Release 2.9.0 - UNRELEASED
integrated ResourceHandler mechanism, and also deprecated the old integrated ResourceHandler mechanism, and also deprecated the old
LCEResourceHandler inteface hierarchy. (Varun Vasudev via vinodkv) LCEResourceHandler inteface hierarchy. (Varun Vasudev via vinodkv)
YARN-4647. Make RegisterNodeManagerRequestPBImpl thread-safe. (kasha)
OPTIMIZATIONS OPTIMIZATIONS
BUG FIXES BUG FIXES

View File

@ -65,14 +65,14 @@ public RegisterNodeManagerRequestPBImpl(RegisterNodeManagerRequestProto proto) {
viaProto = true; viaProto = true;
} }
public RegisterNodeManagerRequestProto getProto() { public synchronized RegisterNodeManagerRequestProto getProto() {
mergeLocalToProto(); mergeLocalToProto();
proto = viaProto ? proto : builder.build(); proto = viaProto ? proto : builder.build();
viaProto = true; viaProto = true;
return proto; return proto;
} }
private void mergeLocalToBuilder() { private synchronized void mergeLocalToBuilder() {
if (this.containerStatuses != null) { if (this.containerStatuses != null) {
addNMContainerStatusesToProto(); addNMContainerStatusesToProto();
} }
@ -107,15 +107,16 @@ private synchronized void addNMContainerStatusesToProto() {
} }
private void mergeLocalToProto() { private synchronized void mergeLocalToProto() {
if (viaProto) if (viaProto) {
maybeInitBuilder(); maybeInitBuilder();
}
mergeLocalToBuilder(); mergeLocalToBuilder();
proto = builder.build(); proto = builder.build();
viaProto = true; viaProto = true;
} }
private void maybeInitBuilder() { private synchronized void maybeInitBuilder() {
if (viaProto || builder == null) { if (viaProto || builder == null) {
builder = RegisterNodeManagerRequestProto.newBuilder(proto); builder = RegisterNodeManagerRequestProto.newBuilder(proto);
} }
@ -124,7 +125,7 @@ private void maybeInitBuilder() {
@Override @Override
public Resource getResource() { public synchronized Resource getResource() {
RegisterNodeManagerRequestProtoOrBuilder p = viaProto ? proto : builder; RegisterNodeManagerRequestProtoOrBuilder p = viaProto ? proto : builder;
if (this.resource != null) { if (this.resource != null) {
return this.resource; return this.resource;
@ -137,7 +138,7 @@ public Resource getResource() {
} }
@Override @Override
public void setResource(Resource resource) { public synchronized void setResource(Resource resource) {
maybeInitBuilder(); maybeInitBuilder();
if (resource == null) if (resource == null)
builder.clearResource(); builder.clearResource();
@ -145,7 +146,7 @@ public void setResource(Resource resource) {
} }
@Override @Override
public NodeId getNodeId() { public synchronized NodeId getNodeId() {
RegisterNodeManagerRequestProtoOrBuilder p = viaProto ? proto : builder; RegisterNodeManagerRequestProtoOrBuilder p = viaProto ? proto : builder;
if (this.nodeId != null) { if (this.nodeId != null) {
return this.nodeId; return this.nodeId;
@ -158,15 +159,16 @@ public NodeId getNodeId() {
} }
@Override @Override
public void setNodeId(NodeId nodeId) { public synchronized void setNodeId(NodeId nodeId) {
maybeInitBuilder(); maybeInitBuilder();
if (nodeId == null) if (nodeId == null) {
builder.clearNodeId(); builder.clearNodeId();
}
this.nodeId = nodeId; this.nodeId = nodeId;
} }
@Override @Override
public int getHttpPort() { public synchronized int getHttpPort() {
RegisterNodeManagerRequestProtoOrBuilder p = viaProto ? proto : builder; RegisterNodeManagerRequestProtoOrBuilder p = viaProto ? proto : builder;
if (!p.hasHttpPort()) { if (!p.hasHttpPort()) {
return 0; return 0;
@ -175,18 +177,18 @@ public int getHttpPort() {
} }
@Override @Override
public void setHttpPort(int httpPort) { public synchronized void setHttpPort(int httpPort) {
maybeInitBuilder(); maybeInitBuilder();
builder.setHttpPort(httpPort); builder.setHttpPort(httpPort);
} }
@Override @Override
public List<ApplicationId> getRunningApplications() { public synchronized List<ApplicationId> getRunningApplications() {
initRunningApplications(); initRunningApplications();
return runningApplications; return runningApplications;
} }
private void initRunningApplications() { private synchronized void initRunningApplications() {
if (this.runningApplications != null) { if (this.runningApplications != null) {
return; return;
} }
@ -199,7 +201,7 @@ private void initRunningApplications() {
} }
@Override @Override
public void setRunningApplications(List<ApplicationId> apps) { public synchronized void setRunningApplications(List<ApplicationId> apps) {
if (apps == null) { if (apps == null) {
return; return;
} }
@ -207,7 +209,7 @@ public void setRunningApplications(List<ApplicationId> apps) {
this.runningApplications.addAll(apps); this.runningApplications.addAll(apps);
} }
private void addRunningApplicationsToProto() { private synchronized void addRunningApplicationsToProto() {
maybeInitBuilder(); maybeInitBuilder();
builder.clearRunningApplications(); builder.clearRunningApplications();
if (runningApplications == null) { if (runningApplications == null) {
@ -241,12 +243,12 @@ public void remove() {
} }
@Override @Override
public List<NMContainerStatus> getNMContainerStatuses() { public synchronized List<NMContainerStatus> getNMContainerStatuses() {
initContainerRecoveryReports(); initContainerRecoveryReports();
return containerStatuses; return containerStatuses;
} }
private void initContainerRecoveryReports() { private synchronized void initContainerRecoveryReports() {
if (this.containerStatuses != null) { if (this.containerStatuses != null) {
return; return;
} }
@ -259,7 +261,7 @@ private void initContainerRecoveryReports() {
} }
@Override @Override
public void setContainerStatuses( public synchronized void setContainerStatuses(
List<NMContainerStatus> containerReports) { List<NMContainerStatus> containerReports) {
if (containerReports == null) { if (containerReports == null) {
return; return;
@ -284,7 +286,7 @@ public boolean equals(Object other) {
} }
@Override @Override
public String getNMVersion() { public synchronized String getNMVersion() {
RegisterNodeManagerRequestProtoOrBuilder p = viaProto ? proto : builder; RegisterNodeManagerRequestProtoOrBuilder p = viaProto ? proto : builder;
if (!p.hasNmVersion()) { if (!p.hasNmVersion()) {
return ""; return "";
@ -293,25 +295,25 @@ public String getNMVersion() {
} }
@Override @Override
public void setNMVersion(String version) { public synchronized void setNMVersion(String version) {
maybeInitBuilder(); maybeInitBuilder();
builder.setNmVersion(version); builder.setNmVersion(version);
} }
@Override @Override
public Set<NodeLabel> getNodeLabels() { public synchronized Set<NodeLabel> getNodeLabels() {
initNodeLabels(); initNodeLabels();
return this.labels; return this.labels;
} }
@Override @Override
public void setNodeLabels(Set<NodeLabel> nodeLabels) { public synchronized void setNodeLabels(Set<NodeLabel> nodeLabels) {
maybeInitBuilder(); maybeInitBuilder();
builder.clearNodeLabels(); builder.clearNodeLabels();
this.labels = nodeLabels; this.labels = nodeLabels;
} }
private void initNodeLabels() { private synchronized void initNodeLabels() {
if (this.labels != null) { if (this.labels != null) {
return; return;
} }
@ -327,43 +329,46 @@ private void initNodeLabels() {
} }
} }
private NodeLabelPBImpl convertFromProtoFormat(NodeLabelProto p) { private static NodeLabelPBImpl convertFromProtoFormat(NodeLabelProto p) {
return new NodeLabelPBImpl(p); return new NodeLabelPBImpl(p);
} }
private NodeLabelProto convertToProtoFormat(NodeLabel t) { private static NodeLabelProto convertToProtoFormat(NodeLabel t) {
return ((NodeLabelPBImpl)t).getProto(); return ((NodeLabelPBImpl)t).getProto();
} }
private ApplicationIdPBImpl convertFromProtoFormat(ApplicationIdProto p) { private static ApplicationIdPBImpl convertFromProtoFormat(
ApplicationIdProto p) {
return new ApplicationIdPBImpl(p); return new ApplicationIdPBImpl(p);
} }
private ApplicationIdProto convertToProtoFormat(ApplicationId t) { private static ApplicationIdProto convertToProtoFormat(ApplicationId t) {
return ((ApplicationIdPBImpl)t).getProto(); return ((ApplicationIdPBImpl)t).getProto();
} }
private NodeIdPBImpl convertFromProtoFormat(NodeIdProto p) { private static NodeIdPBImpl convertFromProtoFormat(NodeIdProto p) {
return new NodeIdPBImpl(p); return new NodeIdPBImpl(p);
} }
private NodeIdProto convertToProtoFormat(NodeId t) { private static NodeIdProto convertToProtoFormat(NodeId t) {
return ((NodeIdPBImpl)t).getProto(); return ((NodeIdPBImpl)t).getProto();
} }
private ResourcePBImpl convertFromProtoFormat(ResourceProto p) { private static ResourcePBImpl convertFromProtoFormat(ResourceProto p) {
return new ResourcePBImpl(p); return new ResourcePBImpl(p);
} }
private ResourceProto convertToProtoFormat(Resource t) { private static ResourceProto convertToProtoFormat(Resource t) {
return ((ResourcePBImpl)t).getProto(); return ((ResourcePBImpl)t).getProto();
} }
private NMContainerStatusPBImpl convertFromProtoFormat(NMContainerStatusProto c) { private static NMContainerStatusPBImpl convertFromProtoFormat(
NMContainerStatusProto c) {
return new NMContainerStatusPBImpl(c); return new NMContainerStatusPBImpl(c);
} }
private NMContainerStatusProto convertToProtoFormat(NMContainerStatus c) { private static NMContainerStatusProto convertToProtoFormat(
NMContainerStatus c) {
return ((NMContainerStatusPBImpl)c).getProto(); return ((NMContainerStatusPBImpl)c).getProto();
} }
} }