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.Priority;
25 import org.apache.hadoop.yarn.api.records.Resource;
26 import org.apache.hadoop.yarn.api.records.ResourceRequest;
27 import org.apache.hadoop.yarn.proto.YarnProtos.PriorityProto;
28 import org.apache.hadoop.yarn.proto.YarnProtos.ResourceProto;
29 import org.apache.hadoop.yarn.proto.YarnProtos.ResourceRequestProto;
30 import org.apache.hadoop.yarn.proto.YarnProtos.ResourceRequestProtoOrBuilder;
31 
32 @Private
33 @Unstable
34 public class ResourceRequestPBImpl extends  ResourceRequest {
35   ResourceRequestProto proto = ResourceRequestProto.getDefaultInstance();
36   ResourceRequestProto.Builder builder = null;
37   boolean viaProto = false;
38 
39   private Priority priority = null;
40   private Resource capability = null;
41 
42 
ResourceRequestPBImpl()43   public ResourceRequestPBImpl() {
44     builder = ResourceRequestProto.newBuilder();
45   }
46 
ResourceRequestPBImpl(ResourceRequestProto proto)47   public ResourceRequestPBImpl(ResourceRequestProto proto) {
48     this.proto = proto;
49     viaProto = true;
50   }
51 
getProto()52   public ResourceRequestProto getProto() {
53       mergeLocalToProto();
54     proto = viaProto ? proto : builder.build();
55     viaProto = true;
56     return proto;
57   }
58 
mergeLocalToBuilder()59   private void mergeLocalToBuilder() {
60     if (this.priority != null) {
61       builder.setPriority(convertToProtoFormat(this.priority));
62     }
63     if (this.capability != null) {
64       builder.setCapability(convertToProtoFormat(this.capability));
65     }
66   }
67 
mergeLocalToProto()68   private void mergeLocalToProto() {
69     if (viaProto)
70       maybeInitBuilder();
71     mergeLocalToBuilder();
72     proto = builder.build();
73     viaProto = true;
74   }
75 
maybeInitBuilder()76   private void maybeInitBuilder() {
77     if (viaProto || builder == null) {
78       builder = ResourceRequestProto.newBuilder(proto);
79     }
80     viaProto = false;
81   }
82 
83 
84   @Override
getPriority()85   public Priority getPriority() {
86     ResourceRequestProtoOrBuilder p = viaProto ? proto : builder;
87     if (this.priority != null) {
88       return this.priority;
89     }
90     if (!p.hasPriority()) {
91       return null;
92     }
93     this.priority = convertFromProtoFormat(p.getPriority());
94     return this.priority;
95   }
96 
97   @Override
setPriority(Priority priority)98   public void setPriority(Priority priority) {
99     maybeInitBuilder();
100     if (priority == null)
101       builder.clearPriority();
102     this.priority = priority;
103   }
104   @Override
getResourceName()105   public String getResourceName() {
106     ResourceRequestProtoOrBuilder p = viaProto ? proto : builder;
107     if (!p.hasResourceName()) {
108       return null;
109     }
110     return (p.getResourceName());
111   }
112 
113   @Override
setResourceName(String resourceName)114   public void setResourceName(String resourceName) {
115     maybeInitBuilder();
116     if (resourceName == null) {
117       builder.clearResourceName();
118       return;
119     }
120     builder.setResourceName((resourceName));
121   }
122   @Override
getCapability()123   public Resource getCapability() {
124     ResourceRequestProtoOrBuilder p = viaProto ? proto : builder;
125     if (this.capability != null) {
126       return this.capability;
127     }
128     if (!p.hasCapability()) {
129       return null;
130     }
131     this.capability = convertFromProtoFormat(p.getCapability());
132     return this.capability;
133   }
134 
135   @Override
setCapability(Resource capability)136   public void setCapability(Resource capability) {
137     maybeInitBuilder();
138     if (capability == null)
139       builder.clearCapability();
140     this.capability = capability;
141   }
142   @Override
getNumContainers()143   public synchronized int getNumContainers() {
144     ResourceRequestProtoOrBuilder p = viaProto ? proto : builder;
145     return (p.getNumContainers());
146   }
147 
148   @Override
setNumContainers(int numContainers)149   public synchronized void setNumContainers(int numContainers) {
150     maybeInitBuilder();
151     builder.setNumContainers((numContainers));
152   }
153 
154   @Override
getRelaxLocality()155   public boolean getRelaxLocality() {
156     ResourceRequestProtoOrBuilder p = viaProto ? proto : builder;
157     return p.getRelaxLocality();
158   }
159 
160   @Override
setRelaxLocality(boolean relaxLocality)161   public void setRelaxLocality(boolean relaxLocality) {
162     maybeInitBuilder();
163     builder.setRelaxLocality(relaxLocality);
164   }
165 
convertFromProtoFormat(PriorityProto p)166   private PriorityPBImpl convertFromProtoFormat(PriorityProto p) {
167     return new PriorityPBImpl(p);
168   }
169 
convertToProtoFormat(Priority t)170   private PriorityProto convertToProtoFormat(Priority t) {
171     return ((PriorityPBImpl)t).getProto();
172   }
173 
convertFromProtoFormat(ResourceProto p)174   private ResourcePBImpl convertFromProtoFormat(ResourceProto p) {
175     return new ResourcePBImpl(p);
176   }
177 
convertToProtoFormat(Resource t)178   private ResourceProto convertToProtoFormat(Resource t) {
179     return ((ResourcePBImpl)t).getProto();
180   }
181 
182   @Override
toString()183   public String toString() {
184     return "{Priority: " + getPriority() + ", Capability: " + getCapability()
185         + ", # Containers: " + getNumContainers()
186         + ", Location: " + getResourceName()
187         + ", Relax Locality: " + getRelaxLocality() + "}";
188   }
189 
190   @Override
getNodeLabelExpression()191   public String getNodeLabelExpression() {
192     ResourceRequestProtoOrBuilder p = viaProto ? proto : builder;
193     if (!p.hasNodeLabelExpression()) {
194       return null;
195     }
196     return (p.getNodeLabelExpression().trim());
197   }
198 
199   @Override
setNodeLabelExpression(String nodeLabelExpression)200   public void setNodeLabelExpression(String nodeLabelExpression) {
201     maybeInitBuilder();
202     if (nodeLabelExpression == null) {
203       builder.clearNodeLabelExpression();
204       return;
205     }
206     builder.setNodeLabelExpression(nodeLabelExpression);
207   }
208 }