Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2015 The Bazel Authors. All rights reserved. |
Janak Ramakrishnan | 24f5e69 | 2015-04-10 19:39:55 +0000 | [diff] [blame] | 2 | // |
| 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. |
| 14 | package com.google.devtools.build.skyframe; |
| 15 | |
| 16 | import com.google.common.annotations.VisibleForTesting; |
Janak Ramakrishnan | 24f5e69 | 2015-04-10 19:39:55 +0000 | [diff] [blame] | 17 | import com.google.common.collect.ImmutableSet; |
| 18 | import com.google.common.collect.Maps; |
shreyax | 246f0aa | 2018-02-23 07:46:54 -0800 | [diff] [blame] | 19 | import com.google.devtools.build.lib.util.GroupedList; |
Janak Ramakrishnan | 24f5e69 | 2015-04-10 19:39:55 +0000 | [diff] [blame] | 20 | import java.util.Collections; |
| 21 | import java.util.Map; |
Janak Ramakrishnan | 24f5e69 | 2015-04-10 19:39:55 +0000 | [diff] [blame] | 22 | import 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 |
| 29 | public abstract class AbstractSkyFunctionEnvironment implements SkyFunction.Environment { |
| 30 | protected boolean valuesMissing = false; |
janakr | bddc514 | 2018-08-08 13:10:36 -0700 | [diff] [blame] | 31 | // 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; |
shreyax | 246f0aa | 2018-02-23 07:46:54 -0800 | [diff] [blame] | 37 | @Nullable private final GroupedList<SkyKey> temporaryDirectDeps; |
| 38 | |
shreyax | 246f0aa | 2018-02-23 07:46:54 -0800 | [diff] [blame] | 39 | 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 Ramakrishnan | 24f5e69 | 2015-04-10 19:39:55 +0000 | [diff] [blame] | 52 | /** Implementations should set {@link #valuesMissing} as necessary. */ |
| 53 | protected abstract Map<SkyKey, ValueOrUntypedException> getValueOrUntypedExceptions( |
ulfjack | be83f13 | 2017-07-18 18:12:09 +0200 | [diff] [blame] | 54 | Iterable<? extends SkyKey> depKeys) throws InterruptedException; |
Janak Ramakrishnan | 24f5e69 | 2015-04-10 19:39:55 +0000 | [diff] [blame] | 55 | |
| 56 | @Override |
| 57 | @Nullable |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 58 | public SkyValue getValue(SkyKey depKey) throws InterruptedException { |
shreyax | c10ad74 | 2018-04-10 12:44:40 -0700 | [diff] [blame] | 59 | return getValues(ImmutableSet.of(depKey)).get(depKey); |
Janak Ramakrishnan | 24f5e69 | 2015-04-10 19:39:55 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | @Override |
| 63 | @Nullable |
| 64 | public <E extends Exception> SkyValue getValueOrThrow(SkyKey depKey, Class<E> exceptionClass) |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 65 | throws E, InterruptedException { |
shreyax | c10ad74 | 2018-04-10 12:44:40 -0700 | [diff] [blame] | 66 | return getValuesOrThrow(ImmutableSet.of(depKey), exceptionClass).get(depKey).get(); |
Janak Ramakrishnan | 24f5e69 | 2015-04-10 19:39:55 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | @Override |
| 70 | @Nullable |
John Field | e12d75d | 2016-01-14 19:29:26 +0000 | [diff] [blame] | 71 | public <E1 extends Exception, E2 extends Exception> SkyValue getValueOrThrow( |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 72 | SkyKey depKey, Class<E1> exceptionClass1, Class<E2> exceptionClass2) |
| 73 | throws E1, E2, InterruptedException { |
shreyax | c10ad74 | 2018-04-10 12:44:40 -0700 | [diff] [blame] | 74 | return getValuesOrThrow(ImmutableSet.of(depKey), exceptionClass1, exceptionClass2) |
| 75 | .get(depKey) |
| 76 | .get(); |
Janak Ramakrishnan | 24f5e69 | 2015-04-10 19:39:55 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | @Override |
| 80 | @Nullable |
John Field | e12d75d | 2016-01-14 19:29:26 +0000 | [diff] [blame] | 81 | 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 Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 86 | Class<E3> exceptionClass3) |
| 87 | throws E1, E2, E3, InterruptedException { |
shreyax | c10ad74 | 2018-04-10 12:44:40 -0700 | [diff] [blame] | 88 | return getValuesOrThrow( |
| 89 | ImmutableSet.of(depKey), exceptionClass1, exceptionClass2, exceptionClass3) |
| 90 | .get(depKey) |
| 91 | .get(); |
Janak Ramakrishnan | 24f5e69 | 2015-04-10 19:39:55 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | @Override |
John Field | e12d75d | 2016-01-14 19:29:26 +0000 | [diff] [blame] | 95 | 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 Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 101 | Class<E4> exceptionClass4) |
| 102 | throws E1, E2, E3, E4, InterruptedException { |
shreyax | c10ad74 | 2018-04-10 12:44:40 -0700 | [diff] [blame] | 103 | return getValuesOrThrow( |
| 104 | ImmutableSet.of(depKey), |
| 105 | exceptionClass1, |
| 106 | exceptionClass2, |
| 107 | exceptionClass3, |
| 108 | exceptionClass4) |
| 109 | .get(depKey) |
| 110 | .get(); |
Janak Ramakrishnan | 24f5e69 | 2015-04-10 19:39:55 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | @Override |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 114 | public < |
| 115 | E1 extends Exception, |
| 116 | E2 extends Exception, |
| 117 | E3 extends Exception, |
| 118 | E4 extends Exception, |
John Field | e12d75d | 2016-01-14 19:29:26 +0000 | [diff] [blame] | 119 | 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 Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 126 | Class<E5> exceptionClass5) |
| 127 | throws E1, E2, E3, E4, E5, InterruptedException { |
shreyax | c10ad74 | 2018-04-10 12:44:40 -0700 | [diff] [blame] | 128 | return getValuesOrThrow( |
| 129 | ImmutableSet.of(depKey), |
| 130 | exceptionClass1, |
| 131 | exceptionClass2, |
| 132 | exceptionClass3, |
| 133 | exceptionClass4, |
| 134 | exceptionClass5) |
| 135 | .get(depKey) |
| 136 | .get(); |
John Field | e12d75d | 2016-01-14 19:29:26 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | @Override |
janakr | 45b308a | 2018-06-08 12:51:58 -0700 | [diff] [blame] | 140 | public Map<SkyKey, SkyValue> getValues(Iterable<? extends SkyKey> depKeys) |
| 141 | throws InterruptedException { |
shreyax | c10ad74 | 2018-04-10 12:44:40 -0700 | [diff] [blame] | 142 | Map<SkyKey, ValueOrUntypedException> valuesOrExceptions = getValueOrUntypedExceptions(depKeys); |
janakr | bddc514 | 2018-08-08 13:10:36 -0700 | [diff] [blame] | 143 | checkValuesMissingBecauseOfFilteredError(valuesOrExceptions, null, null, null, null, null); |
shreyax | c10ad74 | 2018-04-10 12:44:40 -0700 | [diff] [blame] | 144 | return Collections.unmodifiableMap( |
| 145 | Maps.transformValues(valuesOrExceptions, ValueOrUntypedException::getValue)); |
Janak Ramakrishnan | 24f5e69 | 2015-04-10 19:39:55 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public <E extends Exception> Map<SkyKey, ValueOrException<E>> getValuesOrThrow( |
ulfjack | be83f13 | 2017-07-18 18:12:09 +0200 | [diff] [blame] | 150 | Iterable<? extends SkyKey> depKeys, Class<E> exceptionClass) throws InterruptedException { |
shreyax | c10ad74 | 2018-04-10 12:44:40 -0700 | [diff] [blame] | 151 | SkyFunctionException.validateExceptionType(exceptionClass); |
| 152 | Map<SkyKey, ValueOrUntypedException> valuesOrExceptions = getValueOrUntypedExceptions(depKeys); |
janakr | bddc514 | 2018-08-08 13:10:36 -0700 | [diff] [blame] | 153 | checkValuesMissingBecauseOfFilteredError( |
| 154 | valuesOrExceptions, exceptionClass, null, null, null, null); |
shreyax | c10ad74 | 2018-04-10 12:44:40 -0700 | [diff] [blame] | 155 | return Collections.unmodifiableMap( |
| 156 | Maps.transformValues( |
| 157 | valuesOrExceptions, voe -> ValueOrException.fromUntypedException(voe, exceptionClass))); |
Janak Ramakrishnan | 24f5e69 | 2015-04-10 19:39:55 +0000 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | @Override |
John Field | e12d75d | 2016-01-14 19:29:26 +0000 | [diff] [blame] | 161 | public <E1 extends Exception, E2 extends Exception> |
| 162 | Map<SkyKey, ValueOrException2<E1, E2>> getValuesOrThrow( |
ulfjack | be83f13 | 2017-07-18 18:12:09 +0200 | [diff] [blame] | 163 | Iterable<? extends SkyKey> depKeys, Class<E1> exceptionClass1, Class<E2> exceptionClass2) |
| 164 | throws InterruptedException { |
shreyax | c10ad74 | 2018-04-10 12:44:40 -0700 | [diff] [blame] | 165 | SkyFunctionException.validateExceptionType(exceptionClass1); |
| 166 | SkyFunctionException.validateExceptionType(exceptionClass2); |
| 167 | Map<SkyKey, ValueOrUntypedException> valuesOrExceptions = getValueOrUntypedExceptions(depKeys); |
janakr | bddc514 | 2018-08-08 13:10:36 -0700 | [diff] [blame] | 168 | checkValuesMissingBecauseOfFilteredError( |
| 169 | valuesOrExceptions, exceptionClass1, exceptionClass2, null, null, null); |
shreyax | c10ad74 | 2018-04-10 12:44:40 -0700 | [diff] [blame] | 170 | return Collections.unmodifiableMap( |
| 171 | Maps.transformValues( |
| 172 | valuesOrExceptions, |
| 173 | voe -> ValueOrException2.fromUntypedException(voe, exceptionClass1, exceptionClass2))); |
Janak Ramakrishnan | 24f5e69 | 2015-04-10 19:39:55 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | @Override |
John Field | e12d75d | 2016-01-14 19:29:26 +0000 | [diff] [blame] | 177 | public <E1 extends Exception, E2 extends Exception, E3 extends Exception> |
| 178 | Map<SkyKey, ValueOrException3<E1, E2, E3>> getValuesOrThrow( |
ulfjack | be83f13 | 2017-07-18 18:12:09 +0200 | [diff] [blame] | 179 | Iterable<? extends SkyKey> depKeys, |
John Field | e12d75d | 2016-01-14 19:29:26 +0000 | [diff] [blame] | 180 | Class<E1> exceptionClass1, |
| 181 | Class<E2> exceptionClass2, |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 182 | Class<E3> exceptionClass3) |
ulfjack | be83f13 | 2017-07-18 18:12:09 +0200 | [diff] [blame] | 183 | throws InterruptedException { |
shreyax | c10ad74 | 2018-04-10 12:44:40 -0700 | [diff] [blame] | 184 | SkyFunctionException.validateExceptionType(exceptionClass1); |
| 185 | SkyFunctionException.validateExceptionType(exceptionClass2); |
| 186 | SkyFunctionException.validateExceptionType(exceptionClass3); |
| 187 | Map<SkyKey, ValueOrUntypedException> valuesOrExceptions = getValueOrUntypedExceptions(depKeys); |
janakr | bddc514 | 2018-08-08 13:10:36 -0700 | [diff] [blame] | 188 | checkValuesMissingBecauseOfFilteredError( |
shreyax | c10ad74 | 2018-04-10 12:44:40 -0700 | [diff] [blame] | 189 | 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 Field | e12d75d | 2016-01-14 19:29:26 +0000 | [diff] [blame] | 196 | } |
| 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( |
ulfjack | be83f13 | 2017-07-18 18:12:09 +0200 | [diff] [blame] | 201 | Iterable<? extends SkyKey> depKeys, |
John Field | e12d75d | 2016-01-14 19:29:26 +0000 | [diff] [blame] | 202 | Class<E1> exceptionClass1, |
| 203 | Class<E2> exceptionClass2, |
| 204 | Class<E3> exceptionClass3, |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 205 | Class<E4> exceptionClass4) |
ulfjack | be83f13 | 2017-07-18 18:12:09 +0200 | [diff] [blame] | 206 | throws InterruptedException { |
shreyax | c10ad74 | 2018-04-10 12:44:40 -0700 | [diff] [blame] | 207 | SkyFunctionException.validateExceptionType(exceptionClass1); |
| 208 | SkyFunctionException.validateExceptionType(exceptionClass2); |
| 209 | SkyFunctionException.validateExceptionType(exceptionClass3); |
| 210 | SkyFunctionException.validateExceptionType(exceptionClass4); |
| 211 | Map<SkyKey, ValueOrUntypedException> valuesOrExceptions = getValueOrUntypedExceptions(depKeys); |
janakr | bddc514 | 2018-08-08 13:10:36 -0700 | [diff] [blame] | 212 | checkValuesMissingBecauseOfFilteredError( |
shreyax | c10ad74 | 2018-04-10 12:44:40 -0700 | [diff] [blame] | 213 | 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 Ramakrishnan | 24f5e69 | 2015-04-10 19:39:55 +0000 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | @Override |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 228 | public < |
| 229 | E1 extends Exception, |
| 230 | E2 extends Exception, |
| 231 | E3 extends Exception, |
| 232 | E4 extends Exception, |
| 233 | E5 extends Exception> |
John Field | e12d75d | 2016-01-14 19:29:26 +0000 | [diff] [blame] | 234 | Map<SkyKey, ValueOrException5<E1, E2, E3, E4, E5>> getValuesOrThrow( |
ulfjack | be83f13 | 2017-07-18 18:12:09 +0200 | [diff] [blame] | 235 | Iterable<? extends SkyKey> depKeys, |
John Field | e12d75d | 2016-01-14 19:29:26 +0000 | [diff] [blame] | 236 | Class<E1> exceptionClass1, |
| 237 | Class<E2> exceptionClass2, |
| 238 | Class<E3> exceptionClass3, |
| 239 | Class<E4> exceptionClass4, |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 240 | Class<E5> exceptionClass5) |
ulfjack | be83f13 | 2017-07-18 18:12:09 +0200 | [diff] [blame] | 241 | throws InterruptedException { |
shreyax | c10ad74 | 2018-04-10 12:44:40 -0700 | [diff] [blame] | 242 | 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); |
janakr | bddc514 | 2018-08-08 13:10:36 -0700 | [diff] [blame] | 248 | checkValuesMissingBecauseOfFilteredError( |
shreyax | c10ad74 | 2018-04-10 12:44:40 -0700 | [diff] [blame] | 249 | 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 Ramakrishnan | 24f5e69 | 2015-04-10 19:39:55 +0000 | [diff] [blame] | 266 | } |
| 267 | |
shreyax | c10ad74 | 2018-04-10 12:44:40 -0700 | [diff] [blame] | 268 | private < |
| 269 | E1 extends Exception, |
| 270 | E2 extends Exception, |
| 271 | E3 extends Exception, |
| 272 | E4 extends Exception, |
| 273 | E5 extends Exception> |
janakr | bddc514 | 2018-08-08 13:10:36 -0700 | [diff] [blame] | 274 | void checkValuesMissingBecauseOfFilteredError( |
shreyax | c10ad74 | 2018-04-10 12:44:40 -0700 | [diff] [blame] | 275 | 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) { |
janakr | bddc514 | 2018-08-08 13:10:36 -0700 | [diff] [blame] | 281 | if (!errorMightHaveBeenFound) { |
| 282 | // Short-circuit in the common case of no errors. |
| 283 | return; |
| 284 | } |
shreyax | c10ad74 | 2018-04-10 12:44:40 -0700 | [diff] [blame] | 285 | 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 Ramakrishnan | 24f5e69 | 2015-04-10 19:39:55 +0000 | [diff] [blame] | 301 | |
| 302 | @Override |
| 303 | public boolean valuesMissing() { |
| 304 | return valuesMissing; |
| 305 | } |
Janak Ramakrishnan | 24f5e69 | 2015-04-10 19:39:55 +0000 | [diff] [blame] | 306 | } |