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.api.records.impl.pb; 20 21 import java.nio.ByteBuffer; 22 23 import org.apache.hadoop.classification.InterfaceAudience.Private; 24 import org.apache.hadoop.classification.InterfaceStability.Unstable; 25 import org.apache.hadoop.yarn.api.protocolrecords.ApplicationsRequestScope; 26 import org.apache.hadoop.yarn.api.records.AMCommand; 27 import org.apache.hadoop.yarn.api.records.ApplicationAccessType; 28 import org.apache.hadoop.yarn.api.records.ApplicationResourceUsageReport; 29 import org.apache.hadoop.yarn.api.records.ContainerState; 30 import org.apache.hadoop.yarn.api.records.FinalApplicationStatus; 31 import org.apache.hadoop.yarn.api.records.LocalResourceType; 32 import org.apache.hadoop.yarn.api.records.LocalResourceVisibility; 33 import org.apache.hadoop.yarn.api.records.NodeId; 34 import org.apache.hadoop.yarn.api.records.NodeState; 35 import org.apache.hadoop.yarn.api.records.QueueACL; 36 import org.apache.hadoop.yarn.api.records.QueueState; 37 import org.apache.hadoop.yarn.api.records.ReservationRequestInterpreter; 38 import org.apache.hadoop.yarn.api.records.YarnApplicationAttemptState; 39 import org.apache.hadoop.yarn.api.records.YarnApplicationState; 40 import org.apache.hadoop.yarn.proto.YarnProtos.AMCommandProto; 41 import org.apache.hadoop.yarn.proto.YarnProtos.ApplicationAccessTypeProto; 42 import org.apache.hadoop.yarn.proto.YarnProtos.ApplicationResourceUsageReportProto; 43 import org.apache.hadoop.yarn.proto.YarnProtos.ContainerStateProto; 44 import org.apache.hadoop.yarn.proto.YarnProtos.FinalApplicationStatusProto; 45 import org.apache.hadoop.yarn.proto.YarnProtos.LocalResourceTypeProto; 46 import org.apache.hadoop.yarn.proto.YarnProtos.LocalResourceVisibilityProto; 47 import org.apache.hadoop.yarn.proto.YarnProtos.NodeIdProto; 48 import org.apache.hadoop.yarn.proto.YarnProtos.NodeStateProto; 49 import org.apache.hadoop.yarn.proto.YarnProtos.QueueACLProto; 50 import org.apache.hadoop.yarn.proto.YarnProtos.QueueStateProto; 51 import org.apache.hadoop.yarn.proto.YarnProtos.ReservationRequestInterpreterProto; 52 import org.apache.hadoop.yarn.proto.YarnProtos.YarnApplicationAttemptStateProto; 53 import org.apache.hadoop.yarn.proto.YarnProtos.YarnApplicationStateProto; 54 import org.apache.hadoop.yarn.proto.YarnServiceProtos; 55 56 import com.google.protobuf.ByteString; 57 58 @Private 59 @Unstable 60 public class ProtoUtils { 61 62 63 /* 64 * ContainerState 65 */ 66 private static String CONTAINER_STATE_PREFIX = "C_"; convertToProtoFormat(ContainerState e)67 public static ContainerStateProto convertToProtoFormat(ContainerState e) { 68 return ContainerStateProto.valueOf(CONTAINER_STATE_PREFIX + e.name()); 69 } convertFromProtoFormat(ContainerStateProto e)70 public static ContainerState convertFromProtoFormat(ContainerStateProto e) { 71 return ContainerState.valueOf(e.name().replace(CONTAINER_STATE_PREFIX, "")); 72 } 73 74 /* 75 * NodeState 76 */ 77 private static String NODE_STATE_PREFIX = "NS_"; convertToProtoFormat(NodeState e)78 public static NodeStateProto convertToProtoFormat(NodeState e) { 79 return NodeStateProto.valueOf(NODE_STATE_PREFIX + e.name()); 80 } convertFromProtoFormat(NodeStateProto e)81 public static NodeState convertFromProtoFormat(NodeStateProto e) { 82 return NodeState.valueOf(e.name().replace(NODE_STATE_PREFIX, "")); 83 } 84 85 /* 86 * NodeId 87 */ convertToProtoFormat(NodeId e)88 public static NodeIdProto convertToProtoFormat(NodeId e) { 89 return ((NodeIdPBImpl)e).getProto(); 90 } convertFromProtoFormat(NodeIdProto e)91 public static NodeId convertFromProtoFormat(NodeIdProto e) { 92 return new NodeIdPBImpl(e); 93 } 94 95 /* 96 * YarnApplicationState 97 */ convertToProtoFormat(YarnApplicationState e)98 public static YarnApplicationStateProto convertToProtoFormat(YarnApplicationState e) { 99 return YarnApplicationStateProto.valueOf(e.name()); 100 } convertFromProtoFormat(YarnApplicationStateProto e)101 public static YarnApplicationState convertFromProtoFormat(YarnApplicationStateProto e) { 102 return YarnApplicationState.valueOf(e.name()); 103 } 104 105 /* 106 * YarnApplicationAttemptState 107 */ 108 private static String YARN_APPLICATION_ATTEMPT_STATE_PREFIX = "APP_ATTEMPT_"; convertToProtoFormat( YarnApplicationAttemptState e)109 public static YarnApplicationAttemptStateProto convertToProtoFormat( 110 YarnApplicationAttemptState e) { 111 return YarnApplicationAttemptStateProto 112 .valueOf(YARN_APPLICATION_ATTEMPT_STATE_PREFIX + e.name()); 113 } convertFromProtoFormat( YarnApplicationAttemptStateProto e)114 public static YarnApplicationAttemptState convertFromProtoFormat( 115 YarnApplicationAttemptStateProto e) { 116 return YarnApplicationAttemptState.valueOf(e.name().replace( 117 YARN_APPLICATION_ATTEMPT_STATE_PREFIX, "")); 118 } 119 120 /* 121 * ApplicationsRequestScope 122 */ 123 public static YarnServiceProtos.ApplicationsRequestScopeProto convertToProtoFormat(ApplicationsRequestScope e)124 convertToProtoFormat(ApplicationsRequestScope e) { 125 return YarnServiceProtos.ApplicationsRequestScopeProto.valueOf(e.name()); 126 } convertFromProtoFormat(YarnServiceProtos.ApplicationsRequestScopeProto e)127 public static ApplicationsRequestScope convertFromProtoFormat 128 (YarnServiceProtos.ApplicationsRequestScopeProto e) { 129 return ApplicationsRequestScope.valueOf(e.name()); 130 } 131 132 /* 133 * ApplicationResourceUsageReport 134 */ convertToProtoFormat(ApplicationResourceUsageReport e)135 public static ApplicationResourceUsageReportProto convertToProtoFormat(ApplicationResourceUsageReport e) { 136 return ((ApplicationResourceUsageReportPBImpl)e).getProto(); 137 } 138 convertFromProtoFormat(ApplicationResourceUsageReportProto e)139 public static ApplicationResourceUsageReport convertFromProtoFormat(ApplicationResourceUsageReportProto e) { 140 return new ApplicationResourceUsageReportPBImpl(e); 141 } 142 143 /* 144 * FinalApplicationStatus 145 */ 146 private static String FINAL_APPLICATION_STATUS_PREFIX = "APP_"; convertToProtoFormat(FinalApplicationStatus e)147 public static FinalApplicationStatusProto convertToProtoFormat(FinalApplicationStatus e) { 148 return FinalApplicationStatusProto.valueOf(FINAL_APPLICATION_STATUS_PREFIX + e.name()); 149 } convertFromProtoFormat(FinalApplicationStatusProto e)150 public static FinalApplicationStatus convertFromProtoFormat(FinalApplicationStatusProto e) { 151 return FinalApplicationStatus.valueOf(e.name().replace(FINAL_APPLICATION_STATUS_PREFIX, "")); 152 } 153 154 /* 155 * LocalResourceType 156 */ convertToProtoFormat(LocalResourceType e)157 public static LocalResourceTypeProto convertToProtoFormat(LocalResourceType e) { 158 return LocalResourceTypeProto.valueOf(e.name()); 159 } convertFromProtoFormat(LocalResourceTypeProto e)160 public static LocalResourceType convertFromProtoFormat(LocalResourceTypeProto e) { 161 return LocalResourceType.valueOf(e.name()); 162 } 163 164 /* 165 * LocalResourceVisibility 166 */ convertToProtoFormat(LocalResourceVisibility e)167 public static LocalResourceVisibilityProto convertToProtoFormat(LocalResourceVisibility e) { 168 return LocalResourceVisibilityProto.valueOf(e.name()); 169 } convertFromProtoFormat(LocalResourceVisibilityProto e)170 public static LocalResourceVisibility convertFromProtoFormat(LocalResourceVisibilityProto e) { 171 return LocalResourceVisibility.valueOf(e.name()); 172 } 173 174 /* 175 * AMCommand 176 */ convertToProtoFormat(AMCommand e)177 public static AMCommandProto convertToProtoFormat(AMCommand e) { 178 return AMCommandProto.valueOf(e.name()); 179 } convertFromProtoFormat(AMCommandProto e)180 public static AMCommand convertFromProtoFormat(AMCommandProto e) { 181 return AMCommand.valueOf(e.name()); 182 } 183 184 /* 185 * ByteBuffer 186 */ convertFromProtoFormat(ByteString byteString)187 public static ByteBuffer convertFromProtoFormat(ByteString byteString) { 188 int capacity = byteString.asReadOnlyByteBuffer().rewind().remaining(); 189 byte[] b = new byte[capacity]; 190 byteString.asReadOnlyByteBuffer().get(b, 0, capacity); 191 return ByteBuffer.wrap(b); 192 } 193 convertToProtoFormat(ByteBuffer byteBuffer)194 public static ByteString convertToProtoFormat(ByteBuffer byteBuffer) { 195 // return ByteString.copyFrom((ByteBuffer)byteBuffer.duplicate().rewind()); 196 int oldPos = byteBuffer.position(); 197 byteBuffer.rewind(); 198 ByteString bs = ByteString.copyFrom(byteBuffer); 199 byteBuffer.position(oldPos); 200 return bs; 201 } 202 203 /* 204 * QueueState 205 */ 206 private static String QUEUE_STATE_PREFIX = "Q_"; convertToProtoFormat(QueueState e)207 public static QueueStateProto convertToProtoFormat(QueueState e) { 208 return QueueStateProto.valueOf(QUEUE_STATE_PREFIX + e.name()); 209 } convertFromProtoFormat(QueueStateProto e)210 public static QueueState convertFromProtoFormat(QueueStateProto e) { 211 return QueueState.valueOf(e.name().replace(QUEUE_STATE_PREFIX, "")); 212 } 213 214 /* 215 * QueueACL 216 */ 217 private static String QUEUE_ACL_PREFIX = "QACL_"; convertToProtoFormat(QueueACL e)218 public static QueueACLProto convertToProtoFormat(QueueACL e) { 219 return QueueACLProto.valueOf(QUEUE_ACL_PREFIX + e.name()); 220 } convertFromProtoFormat(QueueACLProto e)221 public static QueueACL convertFromProtoFormat(QueueACLProto e) { 222 return QueueACL.valueOf(e.name().replace(QUEUE_ACL_PREFIX, "")); 223 } 224 225 226 /* 227 * ApplicationAccessType 228 */ 229 private static String APP_ACCESS_TYPE_PREFIX = "APPACCESS_"; 230 convertToProtoFormat( ApplicationAccessType e)231 public static ApplicationAccessTypeProto convertToProtoFormat( 232 ApplicationAccessType e) { 233 return ApplicationAccessTypeProto.valueOf(APP_ACCESS_TYPE_PREFIX 234 + e.name()); 235 } 236 convertFromProtoFormat( ApplicationAccessTypeProto e)237 public static ApplicationAccessType convertFromProtoFormat( 238 ApplicationAccessTypeProto e) { 239 return ApplicationAccessType.valueOf(e.name().replace( 240 APP_ACCESS_TYPE_PREFIX, "")); 241 } 242 243 /* 244 * Reservation Request interpreter type 245 */ convertToProtoFormat( ReservationRequestInterpreter e)246 public static ReservationRequestInterpreterProto convertToProtoFormat( 247 ReservationRequestInterpreter e) { 248 return ReservationRequestInterpreterProto.valueOf(e.name()); 249 } 250 convertFromProtoFormat( ReservationRequestInterpreterProto e)251 public static ReservationRequestInterpreter convertFromProtoFormat( 252 ReservationRequestInterpreterProto e) { 253 return ReservationRequestInterpreter.valueOf(e.name()); 254 } 255 256 } 257