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 | package com.google.devtools.build.skyframe; |
| 14 | |
janakr | 4089f8b | 2018-05-22 20:08:41 -0700 | [diff] [blame] | 15 | import com.google.common.annotations.VisibleForTesting; |
| 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; |
janakr | 4089f8b | 2018-05-22 20:08:41 -0700 | [diff] [blame] | 18 | import com.google.common.collect.Iterables; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 19 | import com.google.devtools.build.lib.collect.nestedset.NestedSet; |
| 20 | import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder; |
| 21 | import com.google.devtools.build.lib.collect.nestedset.Order; |
Klaus Aehlig | da0a701 | 2017-06-14 13:40:23 +0200 | [diff] [blame] | 22 | import com.google.devtools.build.lib.events.ExtendedEventHandler.Postable; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 23 | import java.util.Objects; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 24 | import javax.annotation.Nullable; |
| 25 | |
| 26 | /** |
| 27 | * Encapsulation of data stored by {@link NodeEntry} when the value has finished building. |
| 28 | * |
| 29 | * <p>This is intended only for use in alternative {@code MemoizingEvaluator} implementations. |
| 30 | */ |
| 31 | public abstract class ValueWithMetadata implements SkyValue { |
| 32 | protected final SkyValue value; |
| 33 | |
| 34 | private static final NestedSet<TaggedEvents> NO_EVENTS = |
| 35 | NestedSetBuilder.<TaggedEvents>emptySet(Order.STABLE_ORDER); |
| 36 | |
Klaus Aehlig | da0a701 | 2017-06-14 13:40:23 +0200 | [diff] [blame] | 37 | private static final NestedSet<Postable> NO_POSTS = |
| 38 | NestedSetBuilder.<Postable>emptySet(Order.STABLE_ORDER); |
| 39 | |
janakr | 4089f8b | 2018-05-22 20:08:41 -0700 | [diff] [blame] | 40 | private ValueWithMetadata(SkyValue value) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 41 | this.value = value; |
| 42 | } |
| 43 | |
Klaus Aehlig | da0a701 | 2017-06-14 13:40:23 +0200 | [diff] [blame] | 44 | /** |
| 45 | * Builds a value entry value that has an error (and no value value). |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 46 | * |
| 47 | * <p>This is intended only for use in alternative {@code MemoizingEvaluator} implementations. |
| 48 | */ |
Klaus Aehlig | da0a701 | 2017-06-14 13:40:23 +0200 | [diff] [blame] | 49 | public static ValueWithMetadata error( |
| 50 | ErrorInfo errorInfo, |
| 51 | NestedSet<TaggedEvents> transitiveEvents, |
| 52 | NestedSet<Postable> transitivePostables) { |
janakr | 4089f8b | 2018-05-22 20:08:41 -0700 | [diff] [blame] | 53 | return (ValueWithMetadata) normal(null, errorInfo, transitiveEvents, transitivePostables); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | /** |
janakr | 4089f8b | 2018-05-22 20:08:41 -0700 | [diff] [blame] | 57 | * Builds a SkyValue that has a value, and possibly an error, and possibly events/postables. If it |
| 58 | * has only a value, returns just the value in order to save memory. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 59 | * |
Janak Ramakrishnan | 91ad93b | 2015-08-24 17:48:34 +0000 | [diff] [blame] | 60 | * <p>This is public only for use in alternative {@code MemoizingEvaluator} implementations. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 61 | */ |
Janak Ramakrishnan | 91ad93b | 2015-08-24 17:48:34 +0000 | [diff] [blame] | 62 | public static SkyValue normal( |
| 63 | @Nullable SkyValue value, |
| 64 | @Nullable ErrorInfo errorInfo, |
Klaus Aehlig | da0a701 | 2017-06-14 13:40:23 +0200 | [diff] [blame] | 65 | NestedSet<TaggedEvents> transitiveEvents, |
| 66 | NestedSet<Postable> transitivePostables) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 67 | Preconditions.checkState(value != null || errorInfo != null, |
| 68 | "Value and error cannot both be null"); |
| 69 | if (errorInfo == null) { |
Klaus Aehlig | da0a701 | 2017-06-14 13:40:23 +0200 | [diff] [blame] | 70 | return (transitiveEvents.isEmpty() && transitivePostables.isEmpty()) |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 71 | ? value |
Klaus Aehlig | da0a701 | 2017-06-14 13:40:23 +0200 | [diff] [blame] | 72 | : ValueWithEvents.createValueWithEvents(value, transitiveEvents, transitivePostables); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 73 | } |
Klaus Aehlig | da0a701 | 2017-06-14 13:40:23 +0200 | [diff] [blame] | 74 | return new ErrorInfoValue(errorInfo, value, transitiveEvents, transitivePostables); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 75 | } |
| 76 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 77 | @Nullable SkyValue getValue() { |
| 78 | return value; |
| 79 | } |
| 80 | |
| 81 | @Nullable |
| 82 | abstract ErrorInfo getErrorInfo(); |
| 83 | |
Nathan Harmata | cba3d7e | 2015-03-04 23:10:06 +0000 | [diff] [blame] | 84 | public abstract NestedSet<TaggedEvents> getTransitiveEvents(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 85 | |
Klaus Aehlig | da0a701 | 2017-06-14 13:40:23 +0200 | [diff] [blame] | 86 | public abstract NestedSet<Postable> getTransitivePostables(); |
| 87 | |
Nathan Harmata | cba3d7e | 2015-03-04 23:10:06 +0000 | [diff] [blame] | 88 | /** Implementation of {@link ValueWithMetadata} for the value case. */ |
janakr | 4089f8b | 2018-05-22 20:08:41 -0700 | [diff] [blame] | 89 | @VisibleForTesting |
Eric Fellheimer | 26b3d85 | 2015-11-19 02:16:24 +0000 | [diff] [blame] | 90 | public static class ValueWithEvents extends ValueWithMetadata { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 91 | private final NestedSet<TaggedEvents> transitiveEvents; |
Klaus Aehlig | da0a701 | 2017-06-14 13:40:23 +0200 | [diff] [blame] | 92 | private final NestedSet<Postable> transitivePostables; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 93 | |
Klaus Aehlig | da0a701 | 2017-06-14 13:40:23 +0200 | [diff] [blame] | 94 | private ValueWithEvents( |
| 95 | SkyValue value, |
| 96 | NestedSet<TaggedEvents> transitiveEvents, |
| 97 | NestedSet<Postable> transitivePostables) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 98 | super(Preconditions.checkNotNull(value)); |
| 99 | this.transitiveEvents = Preconditions.checkNotNull(transitiveEvents); |
Klaus Aehlig | da0a701 | 2017-06-14 13:40:23 +0200 | [diff] [blame] | 100 | this.transitivePostables = Preconditions.checkNotNull(transitivePostables); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 101 | } |
| 102 | |
janakr | 4089f8b | 2018-05-22 20:08:41 -0700 | [diff] [blame] | 103 | private static ValueWithEvents createValueWithEvents( |
Klaus Aehlig | da0a701 | 2017-06-14 13:40:23 +0200 | [diff] [blame] | 104 | SkyValue value, |
| 105 | NestedSet<TaggedEvents> transitiveEvents, |
| 106 | NestedSet<Postable> transitivePostables) { |
Eric Fellheimer | 26b3d85 | 2015-11-19 02:16:24 +0000 | [diff] [blame] | 107 | if (value instanceof NotComparableSkyValue) { |
Klaus Aehlig | da0a701 | 2017-06-14 13:40:23 +0200 | [diff] [blame] | 108 | return new NotComparableValueWithEvents(value, transitiveEvents, transitivePostables); |
Eric Fellheimer | 26b3d85 | 2015-11-19 02:16:24 +0000 | [diff] [blame] | 109 | } else { |
Klaus Aehlig | da0a701 | 2017-06-14 13:40:23 +0200 | [diff] [blame] | 110 | return new ValueWithEvents(value, transitiveEvents, transitivePostables); |
Eric Fellheimer | 26b3d85 | 2015-11-19 02:16:24 +0000 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 114 | @Nullable |
| 115 | @Override |
| 116 | ErrorInfo getErrorInfo() { return null; } |
| 117 | |
| 118 | @Override |
Nathan Harmata | cba3d7e | 2015-03-04 23:10:06 +0000 | [diff] [blame] | 119 | public NestedSet<TaggedEvents> getTransitiveEvents() { return transitiveEvents; } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 120 | |
Klaus Aehlig | da0a701 | 2017-06-14 13:40:23 +0200 | [diff] [blame] | 121 | @Override |
| 122 | public NestedSet<Postable> getTransitivePostables() { |
| 123 | return transitivePostables; |
| 124 | } |
| 125 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 126 | /** |
| 127 | * We override equals so that if the same value is written to a {@link NodeEntry} twice, it can |
| 128 | * verify that the two values are equal, and avoid incrementing its version. |
| 129 | */ |
| 130 | @Override |
| 131 | public boolean equals(Object o) { |
| 132 | if (this == o) { |
| 133 | return true; |
| 134 | } |
| 135 | if (o == null || getClass() != o.getClass()) { |
| 136 | return false; |
| 137 | } |
| 138 | |
| 139 | ValueWithEvents that = (ValueWithEvents) o; |
| 140 | |
| 141 | // Shallow equals is a middle ground between using default equals, which might miss |
| 142 | // nested sets with the same elements, and deep equality checking, which would be expensive. |
| 143 | // All three choices are sound, since shallow equals and default equals are more |
| 144 | // conservative than deep equals. Using shallow equals means that we may unnecessarily |
| 145 | // consider some values unequal that are actually equal, but this is still a net win over |
| 146 | // deep equals. |
Klaus Aehlig | da0a701 | 2017-06-14 13:40:23 +0200 | [diff] [blame] | 147 | return value.equals(that.value) |
| 148 | && transitiveEvents.shallowEquals(that.transitiveEvents) |
| 149 | && transitivePostables.shallowEquals(that.transitivePostables); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | @Override |
| 153 | public int hashCode() { |
Klaus Aehlig | da0a701 | 2017-06-14 13:40:23 +0200 | [diff] [blame] | 154 | return 31 * value.hashCode() |
| 155 | + transitiveEvents.shallowHashCode() |
| 156 | + 3 * transitivePostables.shallowHashCode(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | @Override |
janakr | 4089f8b | 2018-05-22 20:08:41 -0700 | [diff] [blame] | 160 | public String toString() { |
| 161 | return MoreObjects.toStringHelper(this) |
| 162 | .add("value", value) |
| 163 | .add("transitiveEvents size", Iterables.size(transitiveEvents)) |
| 164 | .add("transitivePostables size", Iterables.size(transitivePostables)) |
| 165 | .toString(); |
| 166 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 167 | } |
| 168 | |
Eric Fellheimer | 26b3d85 | 2015-11-19 02:16:24 +0000 | [diff] [blame] | 169 | private static final class NotComparableValueWithEvents extends ValueWithEvents |
| 170 | implements NotComparableSkyValue { |
Klaus Aehlig | da0a701 | 2017-06-14 13:40:23 +0200 | [diff] [blame] | 171 | private NotComparableValueWithEvents( |
| 172 | SkyValue value, |
| 173 | NestedSet<TaggedEvents> transitiveEvents, |
| 174 | NestedSet<Postable> transitivePostables) { |
| 175 | super(value, transitiveEvents, transitivePostables); |
Eric Fellheimer | 26b3d85 | 2015-11-19 02:16:24 +0000 | [diff] [blame] | 176 | } |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Implementation of {@link ValueWithMetadata} for the error case. |
| 181 | * |
janakr | 9b0b987 | 2018-06-19 09:45:57 -0700 | [diff] [blame] | 182 | * <p>Mark NotComparableSkyValue because it's unlikely that re-evaluation gives the same error. |
Eric Fellheimer | 26b3d85 | 2015-11-19 02:16:24 +0000 | [diff] [blame] | 183 | */ |
| 184 | private static final class ErrorInfoValue extends ValueWithMetadata |
janakr | 9b0b987 | 2018-06-19 09:45:57 -0700 | [diff] [blame] | 185 | implements NotComparableSkyValue { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 186 | |
| 187 | private final ErrorInfo errorInfo; |
| 188 | private final NestedSet<TaggedEvents> transitiveEvents; |
Klaus Aehlig | da0a701 | 2017-06-14 13:40:23 +0200 | [diff] [blame] | 189 | private final NestedSet<Postable> transitivePostables; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 190 | |
Klaus Aehlig | da0a701 | 2017-06-14 13:40:23 +0200 | [diff] [blame] | 191 | public ErrorInfoValue( |
| 192 | ErrorInfo errorInfo, |
| 193 | @Nullable SkyValue value, |
| 194 | NestedSet<TaggedEvents> transitiveEvents, |
| 195 | NestedSet<Postable> transitivePostables) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 196 | super(value); |
| 197 | this.errorInfo = Preconditions.checkNotNull(errorInfo); |
| 198 | this.transitiveEvents = Preconditions.checkNotNull(transitiveEvents); |
Klaus Aehlig | da0a701 | 2017-06-14 13:40:23 +0200 | [diff] [blame] | 199 | this.transitivePostables = Preconditions.checkNotNull(transitivePostables); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | @Nullable |
| 203 | @Override |
| 204 | ErrorInfo getErrorInfo() { return errorInfo; } |
| 205 | |
| 206 | @Override |
Nathan Harmata | cba3d7e | 2015-03-04 23:10:06 +0000 | [diff] [blame] | 207 | public NestedSet<TaggedEvents> getTransitiveEvents() { return transitiveEvents; } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 208 | |
| 209 | @Override |
Klaus Aehlig | da0a701 | 2017-06-14 13:40:23 +0200 | [diff] [blame] | 210 | public NestedSet<Postable> getTransitivePostables() { |
| 211 | return transitivePostables; |
| 212 | } |
| 213 | |
| 214 | @Override |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 215 | public boolean equals(Object o) { |
| 216 | if (this == o) { |
| 217 | return true; |
| 218 | } |
| 219 | if (o == null || getClass() != o.getClass()) { |
| 220 | return false; |
| 221 | } |
| 222 | |
| 223 | ErrorInfoValue that = (ErrorInfoValue) o; |
| 224 | |
| 225 | // Shallow equals is a middle ground between using default equals, which might miss |
| 226 | // nested sets with the same elements, and deep equality checking, which would be expensive. |
| 227 | // All three choices are sound, since shallow equals and default equals are more |
| 228 | // conservative than deep equals. Using shallow equals means that we may unnecessarily |
| 229 | // consider some values unequal that are actually equal, but this is still a net win over |
| 230 | // deep equals. |
| 231 | return Objects.equals(this.value, that.value) |
| 232 | && Objects.equals(this.errorInfo, that.errorInfo) |
Klaus Aehlig | da0a701 | 2017-06-14 13:40:23 +0200 | [diff] [blame] | 233 | && transitiveEvents.shallowEquals(that.transitiveEvents) |
| 234 | && transitivePostables.shallowEquals(that.transitivePostables); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | @Override |
| 238 | public int hashCode() { |
Klaus Aehlig | da0a701 | 2017-06-14 13:40:23 +0200 | [diff] [blame] | 239 | return 31 * Objects.hash(value, errorInfo) |
| 240 | + transitiveEvents.shallowHashCode() |
| 241 | + 3 * transitivePostables.shallowHashCode(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | @Override |
| 245 | public String toString() { |
| 246 | StringBuilder result = new StringBuilder(); |
| 247 | if (value != null) { |
| 248 | result.append("Value: ").append(value); |
| 249 | } |
| 250 | if (errorInfo != null) { |
| 251 | if (result.length() > 0) { |
| 252 | result.append("; "); |
| 253 | } |
| 254 | result.append("Error: ").append(errorInfo); |
| 255 | } |
| 256 | return result.toString(); |
| 257 | } |
| 258 | } |
| 259 | |
shahan | f89e063 | 2018-08-31 11:32:42 -0700 | [diff] [blame] | 260 | @Nullable |
Nathan Harmata | cba3d7e | 2015-03-04 23:10:06 +0000 | [diff] [blame] | 261 | public static SkyValue justValue(SkyValue value) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 262 | if (value instanceof ValueWithMetadata) { |
| 263 | return ((ValueWithMetadata) value).getValue(); |
| 264 | } |
| 265 | return value; |
| 266 | } |
| 267 | |
Nathan Harmata | cba3d7e | 2015-03-04 23:10:06 +0000 | [diff] [blame] | 268 | public static ValueWithMetadata wrapWithMetadata(SkyValue value) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 269 | if (value instanceof ValueWithMetadata) { |
| 270 | return (ValueWithMetadata) value; |
| 271 | } |
Klaus Aehlig | da0a701 | 2017-06-14 13:40:23 +0200 | [diff] [blame] | 272 | return ValueWithEvents.createValueWithEvents(value, NO_EVENTS, NO_POSTS); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | @Nullable |
| 276 | public static ErrorInfo getMaybeErrorInfo(SkyValue value) { |
| 277 | if (value.getClass() == ErrorInfoValue.class) { |
| 278 | return ((ValueWithMetadata) value).getErrorInfo(); |
| 279 | } |
| 280 | return null; |
Janak Ramakrishnan | 1fec40d | 2016-03-21 19:32:17 +0000 | [diff] [blame] | 281 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 282 | |
janakr | 7574d84 | 2018-08-14 21:38:51 -0700 | [diff] [blame] | 283 | @VisibleForTesting |
| 284 | public static NestedSet<TaggedEvents> getEvents(SkyValue value) { |
Janak Ramakrishnan | 1fec40d | 2016-03-21 19:32:17 +0000 | [diff] [blame] | 285 | if (value instanceof ValueWithMetadata) { |
| 286 | return ((ValueWithMetadata) value).getTransitiveEvents(); |
| 287 | } |
| 288 | return NestedSetBuilder.emptySet(Order.STABLE_ORDER); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 289 | } |
Klaus Aehlig | da0a701 | 2017-06-14 13:40:23 +0200 | [diff] [blame] | 290 | |
| 291 | static NestedSet<Postable> getPosts(SkyValue value) { |
| 292 | if (value instanceof ValueWithMetadata) { |
| 293 | return ((ValueWithMetadata) value).getTransitivePostables(); |
| 294 | } |
| 295 | return NestedSetBuilder.emptySet(Order.STABLE_ORDER); |
| 296 | } |
Nathan Harmata | cba3d7e | 2015-03-04 23:10:06 +0000 | [diff] [blame] | 297 | } |