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 22 import org.apache.hadoop.classification.InterfaceAudience.Private; 23 import org.apache.hadoop.classification.InterfaceStability.Unstable; 24 import org.apache.hadoop.yarn.api.records.Resource; 25 import org.apache.hadoop.yarn.proto.YarnProtos.ResourceProto; 26 import org.apache.hadoop.yarn.proto.YarnProtos.ResourceProtoOrBuilder; 27 28 @Private 29 @Unstable 30 public class ResourcePBImpl extends Resource { 31 ResourceProto proto = ResourceProto.getDefaultInstance(); 32 ResourceProto.Builder builder = null; 33 boolean viaProto = false; 34 ResourcePBImpl()35 public ResourcePBImpl() { 36 builder = ResourceProto.newBuilder(); 37 } 38 ResourcePBImpl(ResourceProto proto)39 public ResourcePBImpl(ResourceProto proto) { 40 this.proto = proto; 41 viaProto = true; 42 } 43 getProto()44 public ResourceProto getProto() { 45 proto = viaProto ? proto : builder.build(); 46 viaProto = true; 47 return proto; 48 } 49 maybeInitBuilder()50 private void maybeInitBuilder() { 51 if (viaProto || builder == null) { 52 builder = ResourceProto.newBuilder(proto); 53 } 54 viaProto = false; 55 } 56 57 58 @Override getMemory()59 public int getMemory() { 60 ResourceProtoOrBuilder p = viaProto ? proto : builder; 61 return (p.getMemory()); 62 } 63 64 @Override setMemory(int memory)65 public void setMemory(int memory) { 66 maybeInitBuilder(); 67 builder.setMemory((memory)); 68 } 69 70 @Override getVirtualCores()71 public int getVirtualCores() { 72 ResourceProtoOrBuilder p = viaProto ? proto : builder; 73 return (p.getVirtualCores()); 74 } 75 76 @Override setVirtualCores(int vCores)77 public void setVirtualCores(int vCores) { 78 maybeInitBuilder(); 79 builder.setVirtualCores((vCores)); 80 } 81 82 @Override compareTo(Resource other)83 public int compareTo(Resource other) { 84 int diff = this.getMemory() - other.getMemory(); 85 if (diff == 0) { 86 diff = this.getVirtualCores() - other.getVirtualCores(); 87 } 88 return diff; 89 } 90 91 92 } 93