Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2014 The Bazel Authors. All rights reserved. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [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 | |
janakr | fce927f | 2017-11-03 21:48:32 +0100 | [diff] [blame] | 16 | import com.google.common.base.MoreObjects; |
tomlu | a155b53 | 2017-11-08 20:12:47 +0100 | [diff] [blame^] | 17 | import com.google.common.base.Preconditions; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableList; |
Michajlo Matijkiw | aa05828 | 2015-09-28 22:13:27 +0000 | [diff] [blame] | 19 | import com.google.common.collect.Iterables; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 20 | import com.google.devtools.build.lib.collect.nestedset.NestedSet; |
| 21 | import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder; |
| 22 | import com.google.devtools.build.lib.collect.nestedset.Order; |
| 23 | import com.google.devtools.build.skyframe.SkyFunctionException.ReifiedSkyFunctionException; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 24 | import java.util.Collection; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 25 | import javax.annotation.Nullable; |
| 26 | |
| 27 | /** |
| 28 | * Information about why a {@link SkyValue} failed to evaluate successfully. |
| 29 | * |
| 30 | * <p>This is intended only for use in alternative {@code MemoizingEvaluator} implementations. |
| 31 | */ |
Michajlo Matijkiw | 4e29c83 | 2015-09-30 22:23:25 +0000 | [diff] [blame] | 32 | public class ErrorInfo { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 33 | |
Michajlo Matijkiw | aa05828 | 2015-09-28 22:13:27 +0000 | [diff] [blame] | 34 | /** Create an ErrorInfo from a {@link ReifiedSkyFunctionException}. */ |
Nathan Harmata | 9773168 | 2015-12-09 23:36:22 +0000 | [diff] [blame] | 35 | public static ErrorInfo fromException(ReifiedSkyFunctionException skyFunctionException, |
| 36 | boolean isTransitivelyTransient) { |
Michajlo Matijkiw | aa05828 | 2015-09-28 22:13:27 +0000 | [diff] [blame] | 37 | SkyKey rootCauseSkyKey = skyFunctionException.getRootCauseSkyKey(); |
| 38 | Exception rootCauseException = skyFunctionException.getCause(); |
| 39 | return new ErrorInfo( |
| 40 | NestedSetBuilder.create(Order.STABLE_ORDER, rootCauseSkyKey), |
| 41 | Preconditions.checkNotNull(rootCauseException, "Cause null %s", rootCauseException), |
| 42 | rootCauseSkyKey, |
| 43 | /*cycles=*/ ImmutableList.<CycleInfo>of(), |
nharmata | bea67e9 | 2017-06-16 00:26:27 +0200 | [diff] [blame] | 44 | skyFunctionException.isTransient(), |
Nathan Harmata | 9773168 | 2015-12-09 23:36:22 +0000 | [diff] [blame] | 45 | isTransitivelyTransient || skyFunctionException.isTransient(), |
Michajlo Matijkiw | aa05828 | 2015-09-28 22:13:27 +0000 | [diff] [blame] | 46 | skyFunctionException.isCatastrophic()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 47 | } |
| 48 | |
Michajlo Matijkiw | aa05828 | 2015-09-28 22:13:27 +0000 | [diff] [blame] | 49 | /** Create an ErrorInfo from a {@link CycleInfo}. */ |
Michajlo Matijkiw | e8f7f5e | 2015-09-30 02:39:27 +0000 | [diff] [blame] | 50 | public static ErrorInfo fromCycle(CycleInfo cycleInfo) { |
Michajlo Matijkiw | aa05828 | 2015-09-28 22:13:27 +0000 | [diff] [blame] | 51 | return new ErrorInfo( |
| 52 | /*rootCauses=*/ NestedSetBuilder.<SkyKey>emptySet(Order.STABLE_ORDER), |
| 53 | /*exception=*/ null, |
| 54 | /*rootCauseOfException=*/ null, |
| 55 | ImmutableList.of(cycleInfo), |
nharmata | bea67e9 | 2017-06-16 00:26:27 +0200 | [diff] [blame] | 56 | /*isDirectlyTransient=*/ false, |
| 57 | /*isTransitivelyTransient=*/ false, |
cushon | 03e7018 | 2017-09-15 09:33:27 +0200 | [diff] [blame] | 58 | /* isCatostrophic= */ false); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 59 | } |
| 60 | |
Michajlo Matijkiw | aa05828 | 2015-09-28 22:13:27 +0000 | [diff] [blame] | 61 | /** Create an ErrorInfo from a collection of existing errors. */ |
| 62 | public static ErrorInfo fromChildErrors(SkyKey currentValue, Collection<ErrorInfo> childErrors) { |
| 63 | Preconditions.checkNotNull(currentValue, "currentValue must not be null"); |
| 64 | Preconditions.checkState(!childErrors.isEmpty(), "childErrors may not be empty"); |
| 65 | |
| 66 | NestedSetBuilder<SkyKey> rootCausesBuilder = NestedSetBuilder.stableOrder(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 67 | ImmutableList.Builder<CycleInfo> cycleBuilder = ImmutableList.builder(); |
| 68 | Exception firstException = null; |
| 69 | SkyKey firstChildKey = null; |
nharmata | bea67e9 | 2017-06-16 00:26:27 +0200 | [diff] [blame] | 70 | boolean isTransitivelyTransient = false; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 71 | boolean isCatastrophic = false; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 72 | for (ErrorInfo child : childErrors) { |
| 73 | if (firstException == null) { |
Nathan Harmata | 9773168 | 2015-12-09 23:36:22 +0000 | [diff] [blame] | 74 | // Arbitrarily pick the first error. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 75 | firstException = child.getException(); |
| 76 | firstChildKey = child.getRootCauseOfException(); |
| 77 | } |
Michajlo Matijkiw | aa05828 | 2015-09-28 22:13:27 +0000 | [diff] [blame] | 78 | rootCausesBuilder.addTransitive(child.rootCauses); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 79 | cycleBuilder.addAll(CycleInfo.prepareCycles(currentValue, child.cycles)); |
nharmata | bea67e9 | 2017-06-16 00:26:27 +0200 | [diff] [blame] | 80 | isTransitivelyTransient |= child.isTransitivelyTransient(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 81 | isCatastrophic |= child.isCatastrophic(); |
| 82 | } |
Michajlo Matijkiw | aa05828 | 2015-09-28 22:13:27 +0000 | [diff] [blame] | 83 | |
| 84 | return new ErrorInfo( |
| 85 | rootCausesBuilder.build(), |
| 86 | firstException, |
| 87 | firstChildKey, |
| 88 | cycleBuilder.build(), |
nharmata | bea67e9 | 2017-06-16 00:26:27 +0200 | [diff] [blame] | 89 | /*isDirectlyTransient=*/ false, |
| 90 | isTransitivelyTransient, |
Michajlo Matijkiw | aa05828 | 2015-09-28 22:13:27 +0000 | [diff] [blame] | 91 | isCatastrophic); |
| 92 | } |
| 93 | |
Michajlo Matijkiw | 7241fd6 | 2015-10-05 18:08:57 +0000 | [diff] [blame] | 94 | private final NestedSet<SkyKey> rootCauses; |
Michajlo Matijkiw | aa05828 | 2015-09-28 22:13:27 +0000 | [diff] [blame] | 95 | |
| 96 | @Nullable private final Exception exception; |
| 97 | private final SkyKey rootCauseOfException; |
| 98 | |
| 99 | private final ImmutableList<CycleInfo> cycles; |
| 100 | |
nharmata | bea67e9 | 2017-06-16 00:26:27 +0200 | [diff] [blame] | 101 | private final boolean isDirectlyTransient; |
| 102 | private final boolean isTransitivelyTransient; |
Michajlo Matijkiw | aa05828 | 2015-09-28 22:13:27 +0000 | [diff] [blame] | 103 | private final boolean isCatastrophic; |
| 104 | |
nharmata | bea67e9 | 2017-06-16 00:26:27 +0200 | [diff] [blame] | 105 | public ErrorInfo( |
| 106 | NestedSet<SkyKey> rootCauses, |
| 107 | @Nullable Exception exception, |
| 108 | SkyKey rootCauseOfException, |
| 109 | ImmutableList<CycleInfo> cycles, |
| 110 | boolean isDirectlyTransient, |
| 111 | boolean isTransitivelyTransient, |
Michajlo Matijkiw | aa05828 | 2015-09-28 22:13:27 +0000 | [diff] [blame] | 112 | boolean isCatostrophic) { |
| 113 | Preconditions.checkState(exception != null || !Iterables.isEmpty(cycles), |
| 114 | "At least one of exception and cycles must be non-null/empty, respectively"); |
| 115 | Preconditions.checkState((exception == null) == (rootCauseOfException == null), |
| 116 | "exception and rootCauseOfException must both be null or non-null, got %s %s", |
| 117 | exception, rootCauseOfException); |
| 118 | |
| 119 | this.rootCauses = rootCauses; |
| 120 | this.exception = exception; |
| 121 | this.rootCauseOfException = rootCauseOfException; |
| 122 | this.cycles = cycles; |
nharmata | bea67e9 | 2017-06-16 00:26:27 +0200 | [diff] [blame] | 123 | this.isDirectlyTransient = isDirectlyTransient; |
| 124 | this.isTransitivelyTransient = isTransitivelyTransient; |
Michajlo Matijkiw | aa05828 | 2015-09-28 22:13:27 +0000 | [diff] [blame] | 125 | this.isCatastrophic = isCatostrophic; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public String toString() { |
janakr | fce927f | 2017-11-03 21:48:32 +0100 | [diff] [blame] | 130 | return MoreObjects.toStringHelper(this) |
| 131 | .add("exception", exception) |
| 132 | .add("rootCauses", rootCauses) |
| 133 | .add("cycles", cycles) |
| 134 | .add("isCatastrophic", isCatastrophic) |
| 135 | .add("rootCauseOfException", rootCauseOfException) |
| 136 | .add("isDirectlyTransient", isDirectlyTransient) |
| 137 | .add("isTransitivelyTransient", isTransitivelyTransient) |
| 138 | .toString(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | /** |
| 142 | * The root causes of a value that failed to build are its descendant values that failed to build. |
| 143 | * If a value's descendants all built successfully, but it failed to, its root cause will be |
| 144 | * itself. If a value depends on a cycle, but has no other errors, this method will return |
| 145 | * the empty set. |
| 146 | */ |
| 147 | public Iterable<SkyKey> getRootCauses() { |
| 148 | return rootCauses; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * The exception thrown when building a value. May be null if value's only error is depending |
| 153 | * on a cycle. |
Michajlo Matijkiw | aa05828 | 2015-09-28 22:13:27 +0000 | [diff] [blame] | 154 | * |
| 155 | * <p>The exception is used for reporting and thus may ultimately be rethrown by the caller. |
| 156 | * As well, during a --nokeep_going evaluation, if an error value is encountered from an earlier |
| 157 | * --keep_going build, the exception to be thrown is taken from here. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 158 | */ |
| 159 | @Nullable public Exception getException() { |
| 160 | return exception; |
| 161 | } |
| 162 | |
| 163 | public SkyKey getRootCauseOfException() { |
| 164 | return rootCauseOfException; |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Any cycles found when building this value. |
| 169 | * |
| 170 | * <p>If there are a large number of cycles, only a limited number are returned here. |
| 171 | * |
| 172 | * <p>If this value has a child through which there are multiple paths to the same cycle, only one |
| 173 | * path is returned here. However, if there are multiple paths to the same cycle, each of which |
| 174 | * goes through a different child, each of them is returned here. |
| 175 | */ |
| 176 | public Iterable<CycleInfo> getCycleInfo() { |
| 177 | return cycles; |
| 178 | } |
| 179 | |
| 180 | /** |
nharmata | bea67e9 | 2017-06-16 00:26:27 +0200 | [diff] [blame] | 181 | * Returns true iff the error is directly transient, i.e. if there was a transient error |
| 182 | * encountered during the computation itself. |
| 183 | */ |
| 184 | public boolean isDirectlyTransient() { |
| 185 | return isDirectlyTransient; |
| 186 | } |
| 187 | |
| 188 | /** |
Nathan Harmata | 9773168 | 2015-12-09 23:36:22 +0000 | [diff] [blame] | 189 | * Returns true iff the error is transitively transient, i.e. if retrying the same computation |
| 190 | * could lead to a different result. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 191 | */ |
nharmata | bea67e9 | 2017-06-16 00:26:27 +0200 | [diff] [blame] | 192 | public boolean isTransitivelyTransient() { |
| 193 | return isTransitivelyTransient; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 194 | } |
| 195 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 196 | /** |
| 197 | * Returns true iff the error is catastrophic, i.e. it should halt even for a keepGoing update() |
| 198 | * call. |
| 199 | */ |
| 200 | public boolean isCatastrophic() { |
| 201 | return isCatastrophic; |
| 202 | } |
Janak Ramakrishnan | dad0a10 | 2015-09-18 20:59:28 +0000 | [diff] [blame] | 203 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 204 | } |