1 /** 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 package org.apache.hadoop.yarn.server.api.protocolrecords.impl.pb; 20 21 import org.apache.hadoop.yarn.api.records.ContainerId; 22 import org.apache.hadoop.yarn.api.records.ContainerState; 23 import org.apache.hadoop.yarn.api.records.Priority; 24 import org.apache.hadoop.yarn.api.records.Resource; 25 import org.apache.hadoop.yarn.api.records.impl.pb.ContainerIdPBImpl; 26 import org.apache.hadoop.yarn.api.records.impl.pb.PriorityPBImpl; 27 import org.apache.hadoop.yarn.api.records.impl.pb.ProtoUtils; 28 import org.apache.hadoop.yarn.api.records.impl.pb.ResourcePBImpl; 29 import org.apache.hadoop.yarn.proto.YarnProtos.ContainerIdProto; 30 import org.apache.hadoop.yarn.proto.YarnProtos.ContainerStateProto; 31 import org.apache.hadoop.yarn.proto.YarnProtos.PriorityProto; 32 import org.apache.hadoop.yarn.proto.YarnProtos.ResourceProto; 33 import org.apache.hadoop.yarn.proto.YarnServerCommonServiceProtos.NMContainerStatusProto; 34 import org.apache.hadoop.yarn.proto.YarnServerCommonServiceProtos.NMContainerStatusProtoOrBuilder; 35 import org.apache.hadoop.yarn.server.api.protocolrecords.NMContainerStatus; 36 37 import com.google.protobuf.TextFormat; 38 39 public class NMContainerStatusPBImpl extends NMContainerStatus { 40 41 NMContainerStatusProto proto = NMContainerStatusProto 42 .getDefaultInstance(); 43 NMContainerStatusProto.Builder builder = null; 44 boolean viaProto = false; 45 46 private ContainerId containerId = null; 47 private Resource resource = null; 48 private Priority priority = null; 49 NMContainerStatusPBImpl()50 public NMContainerStatusPBImpl() { 51 builder = NMContainerStatusProto.newBuilder(); 52 } 53 NMContainerStatusPBImpl(NMContainerStatusProto proto)54 public NMContainerStatusPBImpl(NMContainerStatusProto proto) { 55 this.proto = proto; 56 viaProto = true; 57 } 58 getProto()59 public NMContainerStatusProto getProto() { 60 61 mergeLocalToProto(); 62 proto = viaProto ? proto : builder.build(); 63 viaProto = true; 64 return proto; 65 } 66 67 @Override hashCode()68 public int hashCode() { 69 return this.getProto().hashCode(); 70 } 71 72 @Override equals(Object other)73 public boolean equals(Object other) { 74 if (other == null) 75 return false; 76 if (other.getClass().isAssignableFrom(this.getClass())) { 77 return this.getProto().equals(this.getClass().cast(other).getProto()); 78 } 79 return false; 80 } 81 82 @Override toString()83 public String toString() { 84 return TextFormat.shortDebugString(getProto()); 85 } 86 87 @Override getAllocatedResource()88 public Resource getAllocatedResource() { 89 if (this.resource != null) { 90 return this.resource; 91 } 92 NMContainerStatusProtoOrBuilder p = viaProto ? proto : builder; 93 if (!p.hasResource()) { 94 return null; 95 } 96 this.resource = convertFromProtoFormat(p.getResource()); 97 return this.resource; 98 } 99 100 @Override getContainerId()101 public ContainerId getContainerId() { 102 if (this.containerId != null) { 103 return this.containerId; 104 } 105 NMContainerStatusProtoOrBuilder p = viaProto ? proto : builder; 106 if (!p.hasContainerId()) { 107 return null; 108 } 109 this.containerId = convertFromProtoFormat(p.getContainerId()); 110 return this.containerId; 111 } 112 113 @Override getDiagnostics()114 public String getDiagnostics() { 115 NMContainerStatusProtoOrBuilder p = viaProto ? proto : builder; 116 if (!p.hasDiagnostics()) { 117 return null; 118 } 119 return (p.getDiagnostics()); 120 } 121 122 @Override getContainerState()123 public ContainerState getContainerState() { 124 NMContainerStatusProtoOrBuilder p = viaProto ? proto : builder; 125 if (!p.hasContainerState()) { 126 return null; 127 } 128 return convertFromProtoFormat(p.getContainerState()); 129 } 130 131 @Override setAllocatedResource(Resource resource)132 public void setAllocatedResource(Resource resource) { 133 maybeInitBuilder(); 134 if (resource == null) 135 builder.clearResource(); 136 this.resource = resource; 137 } 138 setContainerId(ContainerId containerId)139 public void setContainerId(ContainerId containerId) { 140 maybeInitBuilder(); 141 if (containerId == null) 142 builder.clearContainerId(); 143 this.containerId = containerId; 144 } 145 146 @Override setDiagnostics(String diagnosticsInfo)147 public void setDiagnostics(String diagnosticsInfo) { 148 maybeInitBuilder(); 149 if (diagnosticsInfo == null) { 150 builder.clearDiagnostics(); 151 return; 152 } 153 builder.setDiagnostics(diagnosticsInfo); 154 } 155 156 @Override setContainerState(ContainerState containerState)157 public void setContainerState(ContainerState containerState) { 158 maybeInitBuilder(); 159 if (containerState == null) { 160 builder.clearContainerState(); 161 return; 162 } 163 builder.setContainerState(convertToProtoFormat(containerState)); 164 } 165 166 @Override getContainerExitStatus()167 public int getContainerExitStatus() { 168 NMContainerStatusProtoOrBuilder p = viaProto ? proto : builder; 169 return p.getContainerExitStatus(); 170 } 171 172 @Override setContainerExitStatus(int containerExitStatus)173 public void setContainerExitStatus(int containerExitStatus) { 174 maybeInitBuilder(); 175 builder.setContainerExitStatus(containerExitStatus); 176 } 177 178 @Override getPriority()179 public Priority getPriority() { 180 NMContainerStatusProtoOrBuilder p = viaProto ? proto : builder; 181 if (this.priority != null) { 182 return this.priority; 183 } 184 if (!p.hasPriority()) { 185 return null; 186 } 187 this.priority = convertFromProtoFormat(p.getPriority()); 188 return this.priority; 189 } 190 191 @Override setPriority(Priority priority)192 public void setPriority(Priority priority) { 193 maybeInitBuilder(); 194 if (priority == null) 195 builder.clearPriority(); 196 this.priority = priority; 197 } 198 199 @Override getCreationTime()200 public long getCreationTime() { 201 NMContainerStatusProtoOrBuilder p = viaProto ? proto : builder; 202 return p.getCreationTime(); 203 } 204 205 @Override setCreationTime(long creationTime)206 public void setCreationTime(long creationTime) { 207 maybeInitBuilder(); 208 builder.setCreationTime(creationTime); 209 } 210 mergeLocalToBuilder()211 private void mergeLocalToBuilder() { 212 if (this.containerId != null 213 && !((ContainerIdPBImpl) containerId).getProto().equals( 214 builder.getContainerId())) { 215 builder.setContainerId(convertToProtoFormat(this.containerId)); 216 } 217 218 if (this.resource != null 219 && !((ResourcePBImpl) this.resource).getProto().equals( 220 builder.getResource())) { 221 builder.setResource(convertToProtoFormat(this.resource)); 222 } 223 224 if (this.priority != null) { 225 builder.setPriority(convertToProtoFormat(this.priority)); 226 } 227 } 228 mergeLocalToProto()229 private void mergeLocalToProto() { 230 if (viaProto) 231 maybeInitBuilder(); 232 mergeLocalToBuilder(); 233 proto = builder.build(); 234 viaProto = true; 235 } 236 maybeInitBuilder()237 private void maybeInitBuilder() { 238 if (viaProto || builder == null) { 239 builder = NMContainerStatusProto.newBuilder(proto); 240 } 241 viaProto = false; 242 } 243 convertFromProtoFormat(ContainerIdProto p)244 private ContainerIdPBImpl convertFromProtoFormat(ContainerIdProto p) { 245 return new ContainerIdPBImpl(p); 246 } 247 convertToProtoFormat(ContainerId t)248 private ContainerIdProto convertToProtoFormat(ContainerId t) { 249 return ((ContainerIdPBImpl) t).getProto(); 250 } 251 convertFromProtoFormat(ResourceProto p)252 private ResourcePBImpl convertFromProtoFormat(ResourceProto p) { 253 return new ResourcePBImpl(p); 254 } 255 convertToProtoFormat(Resource t)256 private ResourceProto convertToProtoFormat(Resource t) { 257 return ((ResourcePBImpl) t).getProto(); 258 } 259 260 private ContainerStateProto convertToProtoFormat(ContainerState containerState)261 convertToProtoFormat(ContainerState containerState) { 262 return ProtoUtils.convertToProtoFormat(containerState); 263 } 264 convertFromProtoFormat( ContainerStateProto containerState)265 private ContainerState convertFromProtoFormat( 266 ContainerStateProto containerState) { 267 return ProtoUtils.convertFromProtoFormat(containerState); 268 } 269 convertFromProtoFormat(PriorityProto p)270 private PriorityPBImpl convertFromProtoFormat(PriorityProto p) { 271 return new PriorityPBImpl(p); 272 } 273 convertToProtoFormat(Priority t)274 private PriorityProto convertToProtoFormat(Priority t) { 275 return ((PriorityPBImpl)t).getProto(); 276 } 277 278 } 279