blob: 813dce7df96b5b55277e3045aa2e6700e53b32a0 [file] [log] [blame]
Damien Martin-Guillerezf88f4d82015-09-25 13:56:55 +00001// Copyright 2015 The Bazel Authors. All rights reserved.
Janak Ramakrishnan24f5e692015-04-10 19:39:55 +00002//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14package com.google.devtools.build.skyframe;
15
16import com.google.common.annotations.VisibleForTesting;
Janak Ramakrishnan24f5e692015-04-10 19:39:55 +000017import com.google.common.collect.ImmutableSet;
18import com.google.common.collect.Maps;
shreyax246f0aa2018-02-23 07:46:54 -080019import com.google.devtools.build.lib.util.GroupedList;
Janak Ramakrishnan24f5e692015-04-10 19:39:55 +000020import java.util.Collections;
21import java.util.Map;
Janak Ramakrishnan24f5e692015-04-10 19:39:55 +000022import javax.annotation.Nullable;
23
24/**
25 * Basic implementation of {@link SkyFunction.Environment} in which all convenience methods delegate
26 * to a single abstract method.
27 */
28@VisibleForTesting
29public abstract class AbstractSkyFunctionEnvironment implements SkyFunction.Environment {
30 protected boolean valuesMissing = false;
janakrbddc5142018-08-08 13:10:36 -070031 // Hack for the common case that there are no errors in the retrieved values. In that case, we
32 // don't have to filter out any impermissible exceptions. Hack because we communicate this in an
33 // out-of-band way from #getValueOrUntypedExceptions. It's out-of-band because we don't want to
34 // incur the garbage overhead of returning a more complex data structure from
35 // #getValueOrUntypedExceptions.
36 protected boolean errorMightHaveBeenFound = false;
shreyax246f0aa2018-02-23 07:46:54 -080037 @Nullable private final GroupedList<SkyKey> temporaryDirectDeps;
38
shreyax246f0aa2018-02-23 07:46:54 -080039 public AbstractSkyFunctionEnvironment(@Nullable GroupedList<SkyKey> temporaryDirectDeps) {
40 this.temporaryDirectDeps = temporaryDirectDeps;
41 }
42
43 public AbstractSkyFunctionEnvironment() {
44 this(null);
45 }
46
47 @Override
48 public GroupedList<SkyKey> getTemporaryDirectDeps() {
49 return temporaryDirectDeps;
50 }
51
Janak Ramakrishnan24f5e692015-04-10 19:39:55 +000052 /** Implementations should set {@link #valuesMissing} as necessary. */
53 protected abstract Map<SkyKey, ValueOrUntypedException> getValueOrUntypedExceptions(
ulfjackbe83f132017-07-18 18:12:09 +020054 Iterable<? extends SkyKey> depKeys) throws InterruptedException;
Janak Ramakrishnan24f5e692015-04-10 19:39:55 +000055
56 @Override
57 @Nullable
Janak Ramakrishnan3c0adb22016-08-15 21:54:55 +000058 public SkyValue getValue(SkyKey depKey) throws InterruptedException {
shreyaxc10ad742018-04-10 12:44:40 -070059 return getValues(ImmutableSet.of(depKey)).get(depKey);
Janak Ramakrishnan24f5e692015-04-10 19:39:55 +000060 }
61
62 @Override
63 @Nullable
64 public <E extends Exception> SkyValue getValueOrThrow(SkyKey depKey, Class<E> exceptionClass)
Janak Ramakrishnan3c0adb22016-08-15 21:54:55 +000065 throws E, InterruptedException {
shreyaxc10ad742018-04-10 12:44:40 -070066 return getValuesOrThrow(ImmutableSet.of(depKey), exceptionClass).get(depKey).get();
Janak Ramakrishnan24f5e692015-04-10 19:39:55 +000067 }
68
69 @Override
70 @Nullable
John Fielde12d75d2016-01-14 19:29:26 +000071 public <E1 extends Exception, E2 extends Exception> SkyValue getValueOrThrow(
Janak Ramakrishnan3c0adb22016-08-15 21:54:55 +000072 SkyKey depKey, Class<E1> exceptionClass1, Class<E2> exceptionClass2)
73 throws E1, E2, InterruptedException {
shreyaxc10ad742018-04-10 12:44:40 -070074 return getValuesOrThrow(ImmutableSet.of(depKey), exceptionClass1, exceptionClass2)
75 .get(depKey)
76 .get();
Janak Ramakrishnan24f5e692015-04-10 19:39:55 +000077 }
78
79 @Override
80 @Nullable
John Fielde12d75d2016-01-14 19:29:26 +000081 public <E1 extends Exception, E2 extends Exception, E3 extends Exception>
82 SkyValue getValueOrThrow(
83 SkyKey depKey,
84 Class<E1> exceptionClass1,
85 Class<E2> exceptionClass2,
Janak Ramakrishnan3c0adb22016-08-15 21:54:55 +000086 Class<E3> exceptionClass3)
87 throws E1, E2, E3, InterruptedException {
shreyaxc10ad742018-04-10 12:44:40 -070088 return getValuesOrThrow(
89 ImmutableSet.of(depKey), exceptionClass1, exceptionClass2, exceptionClass3)
90 .get(depKey)
91 .get();
Janak Ramakrishnan24f5e692015-04-10 19:39:55 +000092 }
93
94 @Override
John Fielde12d75d2016-01-14 19:29:26 +000095 public <E1 extends Exception, E2 extends Exception, E3 extends Exception, E4 extends Exception>
96 SkyValue getValueOrThrow(
97 SkyKey depKey,
98 Class<E1> exceptionClass1,
99 Class<E2> exceptionClass2,
100 Class<E3> exceptionClass3,
Janak Ramakrishnan3c0adb22016-08-15 21:54:55 +0000101 Class<E4> exceptionClass4)
102 throws E1, E2, E3, E4, InterruptedException {
shreyaxc10ad742018-04-10 12:44:40 -0700103 return getValuesOrThrow(
104 ImmutableSet.of(depKey),
105 exceptionClass1,
106 exceptionClass2,
107 exceptionClass3,
108 exceptionClass4)
109 .get(depKey)
110 .get();
Janak Ramakrishnan24f5e692015-04-10 19:39:55 +0000111 }
112
113 @Override
Janak Ramakrishnan3c0adb22016-08-15 21:54:55 +0000114 public <
115 E1 extends Exception,
116 E2 extends Exception,
117 E3 extends Exception,
118 E4 extends Exception,
John Fielde12d75d2016-01-14 19:29:26 +0000119 E5 extends Exception>
120 SkyValue getValueOrThrow(
121 SkyKey depKey,
122 Class<E1> exceptionClass1,
123 Class<E2> exceptionClass2,
124 Class<E3> exceptionClass3,
125 Class<E4> exceptionClass4,
Janak Ramakrishnan3c0adb22016-08-15 21:54:55 +0000126 Class<E5> exceptionClass5)
127 throws E1, E2, E3, E4, E5, InterruptedException {
shreyaxc10ad742018-04-10 12:44:40 -0700128 return getValuesOrThrow(
129 ImmutableSet.of(depKey),
130 exceptionClass1,
131 exceptionClass2,
132 exceptionClass3,
133 exceptionClass4,
134 exceptionClass5)
135 .get(depKey)
136 .get();
John Fielde12d75d2016-01-14 19:29:26 +0000137 }
138
139 @Override
janakr45b308a2018-06-08 12:51:58 -0700140 public Map<SkyKey, SkyValue> getValues(Iterable<? extends SkyKey> depKeys)
141 throws InterruptedException {
shreyaxc10ad742018-04-10 12:44:40 -0700142 Map<SkyKey, ValueOrUntypedException> valuesOrExceptions = getValueOrUntypedExceptions(depKeys);
janakrbddc5142018-08-08 13:10:36 -0700143 checkValuesMissingBecauseOfFilteredError(valuesOrExceptions, null, null, null, null, null);
shreyaxc10ad742018-04-10 12:44:40 -0700144 return Collections.unmodifiableMap(
145 Maps.transformValues(valuesOrExceptions, ValueOrUntypedException::getValue));
Janak Ramakrishnan24f5e692015-04-10 19:39:55 +0000146 }
147
148 @Override
149 public <E extends Exception> Map<SkyKey, ValueOrException<E>> getValuesOrThrow(
ulfjackbe83f132017-07-18 18:12:09 +0200150 Iterable<? extends SkyKey> depKeys, Class<E> exceptionClass) throws InterruptedException {
shreyaxc10ad742018-04-10 12:44:40 -0700151 SkyFunctionException.validateExceptionType(exceptionClass);
152 Map<SkyKey, ValueOrUntypedException> valuesOrExceptions = getValueOrUntypedExceptions(depKeys);
janakrbddc5142018-08-08 13:10:36 -0700153 checkValuesMissingBecauseOfFilteredError(
154 valuesOrExceptions, exceptionClass, null, null, null, null);
shreyaxc10ad742018-04-10 12:44:40 -0700155 return Collections.unmodifiableMap(
156 Maps.transformValues(
157 valuesOrExceptions, voe -> ValueOrException.fromUntypedException(voe, exceptionClass)));
Janak Ramakrishnan24f5e692015-04-10 19:39:55 +0000158 }
159
160 @Override
John Fielde12d75d2016-01-14 19:29:26 +0000161 public <E1 extends Exception, E2 extends Exception>
162 Map<SkyKey, ValueOrException2<E1, E2>> getValuesOrThrow(
ulfjackbe83f132017-07-18 18:12:09 +0200163 Iterable<? extends SkyKey> depKeys, Class<E1> exceptionClass1, Class<E2> exceptionClass2)
164 throws InterruptedException {
shreyaxc10ad742018-04-10 12:44:40 -0700165 SkyFunctionException.validateExceptionType(exceptionClass1);
166 SkyFunctionException.validateExceptionType(exceptionClass2);
167 Map<SkyKey, ValueOrUntypedException> valuesOrExceptions = getValueOrUntypedExceptions(depKeys);
janakrbddc5142018-08-08 13:10:36 -0700168 checkValuesMissingBecauseOfFilteredError(
169 valuesOrExceptions, exceptionClass1, exceptionClass2, null, null, null);
shreyaxc10ad742018-04-10 12:44:40 -0700170 return Collections.unmodifiableMap(
171 Maps.transformValues(
172 valuesOrExceptions,
173 voe -> ValueOrException2.fromUntypedException(voe, exceptionClass1, exceptionClass2)));
Janak Ramakrishnan24f5e692015-04-10 19:39:55 +0000174 }
175
176 @Override
John Fielde12d75d2016-01-14 19:29:26 +0000177 public <E1 extends Exception, E2 extends Exception, E3 extends Exception>
178 Map<SkyKey, ValueOrException3<E1, E2, E3>> getValuesOrThrow(
ulfjackbe83f132017-07-18 18:12:09 +0200179 Iterable<? extends SkyKey> depKeys,
John Fielde12d75d2016-01-14 19:29:26 +0000180 Class<E1> exceptionClass1,
181 Class<E2> exceptionClass2,
Janak Ramakrishnan3c0adb22016-08-15 21:54:55 +0000182 Class<E3> exceptionClass3)
ulfjackbe83f132017-07-18 18:12:09 +0200183 throws InterruptedException {
shreyaxc10ad742018-04-10 12:44:40 -0700184 SkyFunctionException.validateExceptionType(exceptionClass1);
185 SkyFunctionException.validateExceptionType(exceptionClass2);
186 SkyFunctionException.validateExceptionType(exceptionClass3);
187 Map<SkyKey, ValueOrUntypedException> valuesOrExceptions = getValueOrUntypedExceptions(depKeys);
janakrbddc5142018-08-08 13:10:36 -0700188 checkValuesMissingBecauseOfFilteredError(
shreyaxc10ad742018-04-10 12:44:40 -0700189 valuesOrExceptions, exceptionClass1, exceptionClass2, exceptionClass3, null, null);
190 return Collections.unmodifiableMap(
191 Maps.transformValues(
192 valuesOrExceptions,
193 voe ->
194 ValueOrException3.fromUntypedException(
195 voe, exceptionClass1, exceptionClass2, exceptionClass3)));
John Fielde12d75d2016-01-14 19:29:26 +0000196 }
197
198 @Override
199 public <E1 extends Exception, E2 extends Exception, E3 extends Exception, E4 extends Exception>
200 Map<SkyKey, ValueOrException4<E1, E2, E3, E4>> getValuesOrThrow(
ulfjackbe83f132017-07-18 18:12:09 +0200201 Iterable<? extends SkyKey> depKeys,
John Fielde12d75d2016-01-14 19:29:26 +0000202 Class<E1> exceptionClass1,
203 Class<E2> exceptionClass2,
204 Class<E3> exceptionClass3,
Janak Ramakrishnan3c0adb22016-08-15 21:54:55 +0000205 Class<E4> exceptionClass4)
ulfjackbe83f132017-07-18 18:12:09 +0200206 throws InterruptedException {
shreyaxc10ad742018-04-10 12:44:40 -0700207 SkyFunctionException.validateExceptionType(exceptionClass1);
208 SkyFunctionException.validateExceptionType(exceptionClass2);
209 SkyFunctionException.validateExceptionType(exceptionClass3);
210 SkyFunctionException.validateExceptionType(exceptionClass4);
211 Map<SkyKey, ValueOrUntypedException> valuesOrExceptions = getValueOrUntypedExceptions(depKeys);
janakrbddc5142018-08-08 13:10:36 -0700212 checkValuesMissingBecauseOfFilteredError(
shreyaxc10ad742018-04-10 12:44:40 -0700213 valuesOrExceptions,
214 exceptionClass1,
215 exceptionClass2,
216 exceptionClass3,
217 exceptionClass4,
218 null);
219 return Collections.unmodifiableMap(
220 Maps.transformValues(
221 valuesOrExceptions,
222 voe ->
223 ValueOrException4.fromUntypedException(
224 voe, exceptionClass1, exceptionClass2, exceptionClass3, exceptionClass4)));
Janak Ramakrishnan24f5e692015-04-10 19:39:55 +0000225 }
226
227 @Override
Janak Ramakrishnan3c0adb22016-08-15 21:54:55 +0000228 public <
229 E1 extends Exception,
230 E2 extends Exception,
231 E3 extends Exception,
232 E4 extends Exception,
233 E5 extends Exception>
John Fielde12d75d2016-01-14 19:29:26 +0000234 Map<SkyKey, ValueOrException5<E1, E2, E3, E4, E5>> getValuesOrThrow(
ulfjackbe83f132017-07-18 18:12:09 +0200235 Iterable<? extends SkyKey> depKeys,
John Fielde12d75d2016-01-14 19:29:26 +0000236 Class<E1> exceptionClass1,
237 Class<E2> exceptionClass2,
238 Class<E3> exceptionClass3,
239 Class<E4> exceptionClass4,
Janak Ramakrishnan3c0adb22016-08-15 21:54:55 +0000240 Class<E5> exceptionClass5)
ulfjackbe83f132017-07-18 18:12:09 +0200241 throws InterruptedException {
shreyaxc10ad742018-04-10 12:44:40 -0700242 SkyFunctionException.validateExceptionType(exceptionClass1);
243 SkyFunctionException.validateExceptionType(exceptionClass2);
244 SkyFunctionException.validateExceptionType(exceptionClass3);
245 SkyFunctionException.validateExceptionType(exceptionClass4);
246 SkyFunctionException.validateExceptionType(exceptionClass5);
247 Map<SkyKey, ValueOrUntypedException> valuesOrExceptions = getValueOrUntypedExceptions(depKeys);
janakrbddc5142018-08-08 13:10:36 -0700248 checkValuesMissingBecauseOfFilteredError(
shreyaxc10ad742018-04-10 12:44:40 -0700249 valuesOrExceptions,
250 exceptionClass1,
251 exceptionClass2,
252 exceptionClass3,
253 exceptionClass4,
254 exceptionClass5);
255 return Collections.unmodifiableMap(
256 Maps.transformValues(
257 valuesOrExceptions,
258 voe ->
259 ValueOrException5.fromUntypedException(
260 voe,
261 exceptionClass1,
262 exceptionClass2,
263 exceptionClass3,
264 exceptionClass4,
265 exceptionClass5)));
Janak Ramakrishnan24f5e692015-04-10 19:39:55 +0000266 }
267
shreyaxc10ad742018-04-10 12:44:40 -0700268 private <
269 E1 extends Exception,
270 E2 extends Exception,
271 E3 extends Exception,
272 E4 extends Exception,
273 E5 extends Exception>
janakrbddc5142018-08-08 13:10:36 -0700274 void checkValuesMissingBecauseOfFilteredError(
shreyaxc10ad742018-04-10 12:44:40 -0700275 Map<SkyKey, ValueOrUntypedException> voes,
276 @Nullable Class<E1> exceptionClass1,
277 @Nullable Class<E2> exceptionClass2,
278 @Nullable Class<E3> exceptionClass3,
279 @Nullable Class<E4> exceptionClass4,
280 @Nullable Class<E5> exceptionClass5) {
janakrbddc5142018-08-08 13:10:36 -0700281 if (!errorMightHaveBeenFound) {
282 // Short-circuit in the common case of no errors.
283 return;
284 }
shreyaxc10ad742018-04-10 12:44:40 -0700285 for (ValueOrUntypedException voe : voes.values()) {
286 SkyValue value = voe.getValue();
287 if (value == null) {
288 Exception e = voe.getException();
289 if (e == null
290 || ((exceptionClass1 == null || !exceptionClass1.isInstance(e))
291 && (exceptionClass2 == null || !exceptionClass2.isInstance(e))
292 && (exceptionClass3 == null || !exceptionClass3.isInstance(e))
293 && (exceptionClass4 == null || !exceptionClass4.isInstance(e))
294 && (exceptionClass5 == null || !exceptionClass5.isInstance(e)))) {
295 valuesMissing = true;
296 return;
297 }
298 }
299 }
300 }
Janak Ramakrishnan24f5e692015-04-10 19:39:55 +0000301
302 @Override
303 public boolean valuesMissing() {
304 return valuesMissing;
305 }
Janak Ramakrishnan24f5e692015-04-10 19:39:55 +0000306}