Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2014 The Bazel Authors. All rights reserved. |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +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 | |
Googler | 2c19a57 | 2015-07-16 08:38:49 +0000 | [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; |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableList; |
| 19 | import com.google.common.collect.ImmutableSet; |
| 20 | import com.google.devtools.build.lib.util.GroupedList; |
| 21 | import com.google.devtools.build.lib.util.GroupedList.GroupedListHelper; |
Janak Ramakrishnan | cb8a97d | 2017-03-23 20:50:08 +0000 | [diff] [blame] | 22 | import com.google.devtools.build.skyframe.KeyToConsolidate.Op; |
| 23 | import com.google.devtools.build.skyframe.KeyToConsolidate.OpToStoreBare; |
janakr | a2d4d3d | 2018-12-10 18:30:08 -0800 | [diff] [blame] | 24 | import java.math.BigInteger; |
Janak Ramakrishnan | cb8a97d | 2017-03-23 20:50:08 +0000 | [diff] [blame] | 25 | import java.util.ArrayList; |
Janak Ramakrishnan | 441dcb1 | 2015-10-09 22:44:31 +0000 | [diff] [blame] | 26 | import java.util.List; |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 27 | import java.util.Set; |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 28 | import javax.annotation.Nullable; |
| 29 | |
| 30 | /** |
| 31 | * In-memory implementation of {@link NodeEntry}. All operations on this class are thread-safe. |
| 32 | * |
| 33 | * <p>Care was taken to provide certain compound operations to avoid certain check-then-act races. |
| 34 | * That means this class is somewhat closely tied to the exact Evaluator implementation. |
| 35 | * |
| 36 | * <p>Consider the example with two threads working on two nodes, where one depends on the other, |
| 37 | * say b depends on a. If a completes first, it's done. If it completes second, it needs to signal |
| 38 | * b, and potentially re-schedule it. If b completes first, it must exit, because it will be |
| 39 | * signaled (and re-scheduled) by a. If it completes second, it must signal (and re-schedule) |
Janak Ramakrishnan | ab10f47 | 2017-03-24 17:08:24 +0000 | [diff] [blame] | 40 | * itself. However, if the Evaluator supported re-entrancy for a node, then this wouldn't have to be |
| 41 | * so strict, because duplicate scheduling would be less problematic. |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 42 | * |
Janak Ramakrishnan | ab10f47 | 2017-03-24 17:08:24 +0000 | [diff] [blame] | 43 | * <p>During its life, a node can go through states as follows: |
| 44 | * |
| 45 | * <ol> |
| 46 | * <li>Non-existent |
ulfjack | 9beabe0 | 2019-02-13 23:41:59 -0800 | [diff] [blame] | 47 | * <li>Just created or marked as affected ({@link #isDone} is false; {@link #isDirty} is false) |
| 48 | * <li>Evaluating ({@link #isDone} is false; {@link #isDirty} is true) |
| 49 | * <li>Done ({@link #isDone} is true; {@link #isDirty} is false) |
Janak Ramakrishnan | ab10f47 | 2017-03-24 17:08:24 +0000 | [diff] [blame] | 50 | * </ol> |
| 51 | * |
| 52 | * <p>The "just created" state is there to allow the {@link EvaluableGraph#createIfAbsentBatch} and |
| 53 | * {@link NodeEntry#addReverseDepAndCheckIfDone} methods to be separate. All callers have to call |
| 54 | * both methods in that order if they want to create a node. The second method transitions the |
| 55 | * current node to the "evaluating" state and returns true only the first time it was called. A |
| 56 | * caller that gets "true" back from that call must start the evaluation of this node, while any |
| 57 | * subsequent callers must not. |
| 58 | * |
| 59 | * <p>An entry is set to "evaluating" as soon as it is scheduled for evaluation. Thus, even a node |
| 60 | * that is never actually built (for instance, a dirty node that is verified as clean) is in the |
| 61 | * "evaluating" state until it is done. |
Nathan Harmata | c533acd | 2015-03-07 03:06:22 +0000 | [diff] [blame] | 62 | * |
ulfjack | 9beabe0 | 2019-02-13 23:41:59 -0800 | [diff] [blame] | 63 | * <p>From the "Done" state, the node can go back to the "marked as affected" state. |
| 64 | * |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 65 | * <p>This class is public only for the benefit of alternative graph implementations outside of the |
| 66 | * package. |
| 67 | */ |
Nathan Harmata | c533acd | 2015-03-07 03:06:22 +0000 | [diff] [blame] | 68 | public class InMemoryNodeEntry implements NodeEntry { |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 69 | |
| 70 | /** Actual data stored in this entry when it is done. */ |
janakr | bf316f7 | 2018-11-28 14:38:44 -0800 | [diff] [blame] | 71 | protected volatile SkyValue value = null; |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 72 | |
| 73 | /** |
Janak Ramakrishnan | 058c4ab | 2016-02-08 21:30:36 +0000 | [diff] [blame] | 74 | * The last version of the graph at which this node's value was changed. In {@link #setValue} it |
| 75 | * may be determined that the value being written to the graph at a given version is the same as |
| 76 | * the already-stored value. In that case, the version will remain the same. The version can be |
| 77 | * thought of as the latest timestamp at which this value was changed. |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 78 | */ |
janakr | bf316f7 | 2018-11-28 14:38:44 -0800 | [diff] [blame] | 79 | protected volatile Version lastChangedVersion = MinimalVersion.INSTANCE; |
Janak Ramakrishnan | 1d22d4c | 2015-11-18 19:28:45 +0000 | [diff] [blame] | 80 | |
| 81 | /** |
| 82 | * Returns the last version this entry was evaluated at, even if it re-evaluated to the same |
Googler | 159a42a | 2018-10-31 13:19:51 -0700 | [diff] [blame] | 83 | * value. When a child signals this entry with the last version it was changed at in {@link |
| 84 | * #signalDep}, this entry need not re-evaluate if the child's version is at most this version, |
| 85 | * even if the {@link #lastChangedVersion} is less than this one. |
Janak Ramakrishnan | 1d22d4c | 2015-11-18 19:28:45 +0000 | [diff] [blame] | 86 | * |
Googler | 159a42a | 2018-10-31 13:19:51 -0700 | [diff] [blame] | 87 | * @see #signalDep(Version, SkyKey) |
Janak Ramakrishnan | 1d22d4c | 2015-11-18 19:28:45 +0000 | [diff] [blame] | 88 | */ |
| 89 | protected Version lastEvaluatedVersion = MinimalVersion.INSTANCE; |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 90 | |
| 91 | /** |
Janak Ramakrishnan | 5093499 | 2016-07-06 17:09:51 +0000 | [diff] [blame] | 92 | * This object represents the direct deps of the node, in groups if the {@code SkyFunction} |
janakr | 1cde872 | 2017-10-10 03:22:21 +0200 | [diff] [blame] | 93 | * requested them that way. It contains either the in-progress direct deps, stored as a {@code |
| 94 | * GroupedList<SkyKey>} before the node is finished building, or the full direct deps, compressed |
| 95 | * in a memory-efficient way (via {@link GroupedList#compress}, after the node is done. |
Janak Ramakrishnan | 5093499 | 2016-07-06 17:09:51 +0000 | [diff] [blame] | 96 | * |
| 97 | * <p>It is initialized lazily in getTemporaryDirectDeps() to save a little bit more memory. |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 98 | */ |
janakr | 1cde872 | 2017-10-10 03:22:21 +0200 | [diff] [blame] | 99 | protected Object directDeps = null; |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 100 | |
| 101 | /** |
| 102 | * This list stores the reverse dependencies of this node that have been declared so far. |
| 103 | * |
| 104 | * <p>In case of a single object we store the object unwrapped, without the list, for |
| 105 | * memory-efficiency. |
Janak Ramakrishnan | cb8a97d | 2017-03-23 20:50:08 +0000 | [diff] [blame] | 106 | * |
| 107 | * <p>When an entry is being re-evaluated, this object stores the reverse deps from the previous |
| 108 | * evaluation. At the end of evaluation, the changed reverse dep operations from {@link |
| 109 | * #reverseDepsDataToConsolidate} are merged in here. |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 110 | */ |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 111 | protected Object reverseDeps = ImmutableList.of(); |
| 112 | |
| 113 | /** |
Janak Ramakrishnan | cb8a97d | 2017-03-23 20:50:08 +0000 | [diff] [blame] | 114 | * This list stores objects returned by {@link KeyToConsolidate#create}. Morally they are {@link |
| 115 | * KeyToConsolidate} objects, but since some operations are stored bare, we can only declare that |
| 116 | * this list holds {@link Object} references. Created lazily to save memory. |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 117 | * |
Janak Ramakrishnan | cb8a97d | 2017-03-23 20:50:08 +0000 | [diff] [blame] | 118 | * <p>This list serves double duty. For a done node, when a reverse dep is removed, checked for |
| 119 | * presence, or possibly added, we store the mutation in this object instead of immediately doing |
| 120 | * the operation. That is because removals/checks in reverseDeps are O(N). Originally reverseDeps |
| 121 | * was a HashSet, but because of memory consumption we switched to a list. |
| 122 | * |
| 123 | * <p>Internally, {@link ReverseDepsUtility} consolidates this data periodically, and when the set |
| 124 | * of reverse deps is requested. While this operation is not free, it can be done more effectively |
Janak Ramakrishnan | 4f487f4 | 2015-11-23 18:51:55 +0000 | [diff] [blame] | 125 | * than trying to remove/check each dirty reverse dependency individually (O(N) each time). |
Janak Ramakrishnan | cb8a97d | 2017-03-23 20:50:08 +0000 | [diff] [blame] | 126 | * |
| 127 | * <p>When the node entry is evaluating, this list serves to declare the reverse dep operations |
| 128 | * that have taken place on it during this evaluation. When evaluation finishes, this list will be |
| 129 | * merged into the existing reverse deps if any, but furthermore, this list will also be used to |
| 130 | * calculate the set of reverse deps to signal when this entry finishes evaluation. That is done |
| 131 | * by {@link ReverseDepsUtility#consolidateDataAndReturnNewElements}. |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 132 | */ |
Janak Ramakrishnan | 441dcb1 | 2015-10-09 22:44:31 +0000 | [diff] [blame] | 133 | private List<Object> reverseDepsDataToConsolidate = null; |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 134 | |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 135 | /** |
Janak Ramakrishnan | ab10f47 | 2017-03-24 17:08:24 +0000 | [diff] [blame] | 136 | * Object encapsulating dirty state of the object between when it is marked dirty and |
| 137 | * re-evaluated. |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 138 | */ |
janakr | 5b4f237 | 2019-01-08 17:12:03 -0800 | [diff] [blame] | 139 | @Nullable protected volatile DirtyBuildingState dirtyBuildingState = null; |
Janak Ramakrishnan | ab10f47 | 2017-03-24 17:08:24 +0000 | [diff] [blame] | 140 | |
Googler | baefeab | 2019-04-30 17:12:55 -0700 | [diff] [blame] | 141 | /** Construct a InMemoryNodeEntry. Use ONLY in Skyframe evaluation and graph implementations. */ |
| 142 | public InMemoryNodeEntry() {} |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 143 | |
janakr | 4f7be0f | 2017-10-18 11:45:48 -0400 | [diff] [blame] | 144 | // Public only for use in alternate graph implementations. |
janakr | 1cde872 | 2017-10-10 03:22:21 +0200 | [diff] [blame] | 145 | public KeepEdgesPolicy keepEdges() { |
| 146 | return KeepEdgesPolicy.ALL; |
| 147 | } |
| 148 | |
| 149 | private boolean keepReverseDeps() { |
| 150 | return keepEdges() == KeepEdgesPolicy.ALL; |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 151 | } |
| 152 | |
ulfjack | 9beabe0 | 2019-02-13 23:41:59 -0800 | [diff] [blame] | 153 | private boolean isEvaluating() { |
| 154 | return dirtyBuildingState != null; |
| 155 | } |
| 156 | |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 157 | @Override |
Shreya Bhattarai | cc1b9b3 | 2017-01-06 18:19:25 +0000 | [diff] [blame] | 158 | public boolean isDone() { |
ulfjack | 9beabe0 | 2019-02-13 23:41:59 -0800 | [diff] [blame] | 159 | return value != null && dirtyBuildingState == null; |
| 160 | } |
| 161 | |
| 162 | @Override |
| 163 | public synchronized boolean isReady() { |
| 164 | Preconditions.checkState(!isDone(), "can't be ready if done: %s", this); |
| 165 | Preconditions.checkState(isEvaluating(), this); |
| 166 | return dirtyBuildingState.isReady(getNumTemporaryDirectDeps()); |
| 167 | } |
| 168 | |
| 169 | @Override |
| 170 | public synchronized boolean isDirty() { |
| 171 | return !isDone() && dirtyBuildingState != null; |
| 172 | } |
| 173 | |
| 174 | @Override |
| 175 | public synchronized boolean isChanged() { |
| 176 | return !isDone() && dirtyBuildingState != null && dirtyBuildingState.isChanged(); |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | @Override |
janakr | 19e03d7 | 2018-01-10 13:13:00 -0800 | [diff] [blame] | 180 | public SkyValue getValue() { |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 181 | Preconditions.checkState(isDone(), "no value until done. ValueEntry: %s", this); |
| 182 | return ValueWithMetadata.justValue(value); |
| 183 | } |
| 184 | |
| 185 | @Override |
mschaller | a8926b7 | 2018-06-28 11:53:36 -0700 | [diff] [blame] | 186 | @Nullable |
janakr | 19e03d7 | 2018-01-10 13:13:00 -0800 | [diff] [blame] | 187 | public SkyValue getValueMaybeWithMetadata() { |
Janak Ramakrishnan | 0afd453 | 2015-08-24 17:27:48 +0000 | [diff] [blame] | 188 | return value; |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | @Override |
janakr | 19e03d7 | 2018-01-10 13:13:00 -0800 | [diff] [blame] | 192 | public SkyValue toValue() { |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 193 | if (isDone()) { |
| 194 | return getErrorInfo() == null ? getValue() : null; |
| 195 | } else if (isChanged() || isDirty()) { |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 196 | SkyValue lastBuildValue = null; |
| 197 | try { |
ulfjack | 9beabe0 | 2019-02-13 23:41:59 -0800 | [diff] [blame] | 198 | lastBuildValue = dirtyBuildingState.getLastBuildValue(); |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 199 | } catch (InterruptedException e) { |
| 200 | throw new IllegalStateException("Interruption unexpected: " + this, e); |
| 201 | } |
| 202 | return (lastBuildValue == null) ? null : ValueWithMetadata.justValue(lastBuildValue); |
Janak Ramakrishnan | 5411129 | 2015-09-09 21:44:07 +0000 | [diff] [blame] | 203 | } else { |
| 204 | // Value has not finished evaluating. It's probably about to be cleaned from the graph. |
| 205 | return null; |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 206 | } |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | @Override |
Googler | b808db4 | 2019-05-14 16:32:30 -0700 | [diff] [blame] | 210 | public Iterable<SkyKey> getDirectDeps() { |
janakr | 4d6c0ab | 2019-04-29 14:46:53 -0700 | [diff] [blame] | 211 | return GroupedList.compressedToIterable(getCompressedDirectDepsForDoneEntry()); |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Googler | b808db4 | 2019-05-14 16:32:30 -0700 | [diff] [blame] | 214 | @Override |
| 215 | public int getNumberOfDirectDepGroups() { |
| 216 | return GroupedList.numGroups(getCompressedDirectDepsForDoneEntry()); |
| 217 | } |
| 218 | |
janakr | 4d6c0ab | 2019-04-29 14:46:53 -0700 | [diff] [blame] | 219 | /** Returns the compressed {@link GroupedList} of direct deps. Can only be called when done. */ |
Googler | b808db4 | 2019-05-14 16:32:30 -0700 | [diff] [blame] | 220 | public final synchronized @GroupedList.Compressed Object getCompressedDirectDepsForDoneEntry() { |
janakr | 1cde872 | 2017-10-10 03:22:21 +0200 | [diff] [blame] | 221 | assertKeepDeps(); |
Janak Ramakrishnan | 5093499 | 2016-07-06 17:09:51 +0000 | [diff] [blame] | 222 | Preconditions.checkState(isDone(), "no deps until done. NodeEntry: %s", this); |
Googler | baefeab | 2019-04-30 17:12:55 -0700 | [diff] [blame] | 223 | Preconditions.checkNotNull(directDeps, "deps can't be null: %s", this); |
| 224 | return GroupedList.castAsCompressed(directDeps); |
Nathan Harmata | df22aaf | 2015-03-06 20:44:46 +0000 | [diff] [blame] | 225 | } |
| 226 | |
shreyax | 5d63436 | 2017-11-29 11:31:49 -0800 | [diff] [blame] | 227 | public int getNumDirectDeps() { |
Googler | baefeab | 2019-04-30 17:12:55 -0700 | [diff] [blame] | 228 | return GroupedList.numElements(getCompressedDirectDepsForDoneEntry()); |
shreyax | 5d63436 | 2017-11-29 11:31:49 -0800 | [diff] [blame] | 229 | } |
| 230 | |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 231 | @Override |
| 232 | @Nullable |
| 233 | public synchronized ErrorInfo getErrorInfo() { |
Janak Ramakrishnan | 5093499 | 2016-07-06 17:09:51 +0000 | [diff] [blame] | 234 | Preconditions.checkState(isDone(), "no errors until done. NodeEntry: %s", this); |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 235 | return ValueWithMetadata.getMaybeErrorInfo(value); |
| 236 | } |
| 237 | |
Janak Ramakrishnan | 4f487f4 | 2015-11-23 18:51:55 +0000 | [diff] [blame] | 238 | /** |
Janak Ramakrishnan | 772b5bb | 2016-06-29 00:20:36 +0000 | [diff] [blame] | 239 | * Puts entry in "done" state, as checked by {@link #isDone}. Subclasses that override one may |
| 240 | * need to override the other. |
Janak Ramakrishnan | 4f487f4 | 2015-11-23 18:51:55 +0000 | [diff] [blame] | 241 | */ |
| 242 | protected void markDone() { |
Janak Ramakrishnan | ab10f47 | 2017-03-24 17:08:24 +0000 | [diff] [blame] | 243 | dirtyBuildingState = null; |
Janak Ramakrishnan | 4f487f4 | 2015-11-23 18:51:55 +0000 | [diff] [blame] | 244 | } |
| 245 | |
ulfjack | 20eeaad | 2019-02-18 02:51:55 -0800 | [diff] [blame] | 246 | @Override |
| 247 | public synchronized void addExternalDep() { |
| 248 | Preconditions.checkNotNull(dirtyBuildingState, this); |
| 249 | dirtyBuildingState.addExternalDep(); |
| 250 | } |
| 251 | |
janakr | 1cde872 | 2017-10-10 03:22:21 +0200 | [diff] [blame] | 252 | protected final synchronized Set<SkyKey> setStateFinishedAndReturnReverseDepsToSignal() { |
Janak Ramakrishnan | cb8a97d | 2017-03-23 20:50:08 +0000 | [diff] [blame] | 253 | Set<SkyKey> reverseDepsToSignal = |
| 254 | ReverseDepsUtility.consolidateDataAndReturnNewElements(this, getOpToStoreBare()); |
Janak Ramakrishnan | 5093499 | 2016-07-06 17:09:51 +0000 | [diff] [blame] | 255 | this.directDeps = getTemporaryDirectDeps().compress(); |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 256 | |
Janak Ramakrishnan | 4f487f4 | 2015-11-23 18:51:55 +0000 | [diff] [blame] | 257 | markDone(); |
janakr | 1cde872 | 2017-10-10 03:22:21 +0200 | [diff] [blame] | 258 | postProcessAfterDone(); |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 259 | return reverseDepsToSignal; |
| 260 | } |
| 261 | |
janakr | 1cde872 | 2017-10-10 03:22:21 +0200 | [diff] [blame] | 262 | protected void postProcessAfterDone() {} |
| 263 | |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 264 | @Override |
| 265 | public synchronized Set<SkyKey> getInProgressReverseDeps() { |
| 266 | Preconditions.checkState(!isDone(), this); |
Janak Ramakrishnan | cb8a97d | 2017-03-23 20:50:08 +0000 | [diff] [blame] | 267 | return ReverseDepsUtility.returnNewElements(this, getOpToStoreBare()); |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 268 | } |
| 269 | |
Googler | 1c12170 | 2018-11-01 14:04:09 -0700 | [diff] [blame] | 270 | // In this method it is critical that this.lastChangedVersion is set prior to this.value because |
| 271 | // although this method itself is synchronized, there are unsynchronized consumers of the version |
| 272 | // and the value. |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 273 | @Override |
janakr | a2d4d3d | 2018-12-10 18:30:08 -0800 | [diff] [blame] | 274 | public synchronized Set<SkyKey> setValue( |
| 275 | SkyValue value, Version version, DepFingerprintList depFingerprintList) |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 276 | throws InterruptedException { |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 277 | Preconditions.checkState(isReady(), "%s %s", this, value); |
janakr | a2d4d3d | 2018-12-10 18:30:08 -0800 | [diff] [blame] | 278 | if (depFingerprintList != null) { |
| 279 | logError( |
| 280 | new IllegalStateException( |
| 281 | String.format( |
| 282 | "Expect no depFingerprintList here: %s %s %s %s", |
| 283 | this, depFingerprintList, value, version))); |
| 284 | } |
janakr | 5469139 | 2018-10-03 14:05:45 -0700 | [diff] [blame] | 285 | assertVersionCompatibleWhenSettingValue(version, value); |
Janak Ramakrishnan | 1d22d4c | 2015-11-18 19:28:45 +0000 | [diff] [blame] | 286 | this.lastEvaluatedVersion = version; |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 287 | |
janakr | a2d4d3d | 2018-12-10 18:30:08 -0800 | [diff] [blame] | 288 | if (!isEligibleForChangePruningOnUnchangedValue()) { |
Googler | 159a42a | 2018-10-31 13:19:51 -0700 | [diff] [blame] | 289 | this.lastChangedVersion = version; |
| 290 | this.value = value; |
ulfjack | 9beabe0 | 2019-02-13 23:41:59 -0800 | [diff] [blame] | 291 | } else if (dirtyBuildingState.unchangedFromLastBuild(value)) { |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 292 | // If the value is the same as before, just use the old value. Note that we don't use the new |
| 293 | // value, because preserving == equality is even better than .equals() equality. |
ulfjack | 9beabe0 | 2019-02-13 23:41:59 -0800 | [diff] [blame] | 294 | this.value = dirtyBuildingState.getLastBuildValue(); |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 295 | } else { |
ulfjack | 9beabe0 | 2019-02-13 23:41:59 -0800 | [diff] [blame] | 296 | boolean forcedRebuild = dirtyBuildingState.getDirtyState() == DirtyState.FORCED_REBUILDING; |
shahan | d4a6ca0 | 2018-10-03 11:41:28 -0700 | [diff] [blame] | 297 | if (!forcedRebuild && this.lastChangedVersion.equals(version)) { |
Googler | 1c12170 | 2018-11-01 14:04:09 -0700 | [diff] [blame] | 298 | logError( |
| 299 | new ChangedValueAtSameVersionException(this.lastChangedVersion, version, value, this)); |
shahan | d4a6ca0 | 2018-10-03 11:41:28 -0700 | [diff] [blame] | 300 | } |
| 301 | // If this is a new value, or it has changed since the last build, set the version to the |
| 302 | // current graph version. |
Janak Ramakrishnan | 1d22d4c | 2015-11-18 19:28:45 +0000 | [diff] [blame] | 303 | this.lastChangedVersion = version; |
Googler | 1c12170 | 2018-11-01 14:04:09 -0700 | [diff] [blame] | 304 | this.value = value; |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 305 | } |
Janak Ramakrishnan | 772b5bb | 2016-06-29 00:20:36 +0000 | [diff] [blame] | 306 | return setStateFinishedAndReturnReverseDepsToSignal(); |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 307 | } |
| 308 | |
Googler | 159a42a | 2018-10-31 13:19:51 -0700 | [diff] [blame] | 309 | /** |
janakr | a2d4d3d | 2018-12-10 18:30:08 -0800 | [diff] [blame] | 310 | * Returns {@code true} if this node is eligible to be change pruned when its value has not |
Googler | 159a42a | 2018-10-31 13:19:51 -0700 | [diff] [blame] | 311 | * changed from the last build. |
| 312 | * |
janakr | a2d4d3d | 2018-12-10 18:30:08 -0800 | [diff] [blame] | 313 | * <p>Implementations need not check whether the value has changed - this will only be called if |
| 314 | * the value has not changed. |
Googler | 159a42a | 2018-10-31 13:19:51 -0700 | [diff] [blame] | 315 | */ |
Googler | 3e145b3 | 2019-01-07 09:48:20 -0800 | [diff] [blame] | 316 | public boolean isEligibleForChangePruningOnUnchangedValue() { |
Googler | 159a42a | 2018-10-31 13:19:51 -0700 | [diff] [blame] | 317 | return true; |
| 318 | } |
| 319 | |
janakr | 5469139 | 2018-10-03 14:05:45 -0700 | [diff] [blame] | 320 | protected void assertVersionCompatibleWhenSettingValue( |
| 321 | Version version, SkyValue valueForDebugging) { |
| 322 | if (!this.lastChangedVersion.atMost(version)) { |
| 323 | logError( |
| 324 | new IllegalStateException("Bad ch: " + this + ", " + version + ", " + valueForDebugging)); |
| 325 | } |
| 326 | if (!this.lastEvaluatedVersion.atMost(version)) { |
| 327 | logError( |
| 328 | new IllegalStateException("Bad ev: " + this + ", " + version + ", " + valueForDebugging)); |
| 329 | } |
| 330 | } |
| 331 | |
Googler | 0eedeba | 2018-10-31 14:27:31 -0700 | [diff] [blame] | 332 | /** An exception indicating that the node's value changed but its version did not. */ |
| 333 | public static final class ChangedValueAtSameVersionException extends IllegalStateException { |
Googler | cdb1d12 | 2018-11-05 15:38:17 -0800 | [diff] [blame] | 334 | private final SkyValue newValue; |
| 335 | |
Googler | 0eedeba | 2018-10-31 14:27:31 -0700 | [diff] [blame] | 336 | private ChangedValueAtSameVersionException( |
Googler | 1c12170 | 2018-11-01 14:04:09 -0700 | [diff] [blame] | 337 | Version lastChangedVersion, |
| 338 | Version newVersion, |
| 339 | SkyValue newValue, |
| 340 | InMemoryNodeEntry nodeEntry) { |
Googler | 0eedeba | 2018-10-31 14:27:31 -0700 | [diff] [blame] | 341 | super( |
| 342 | String.format( |
Googler | 1c12170 | 2018-11-01 14:04:09 -0700 | [diff] [blame] | 343 | "Changed value but with the same version? " |
| 344 | + "lastChangedVersion: %s, newVersion: %s newValue: %s, nodeEntry: %s", |
| 345 | lastChangedVersion, newVersion, newValue, nodeEntry)); |
Googler | cdb1d12 | 2018-11-05 15:38:17 -0800 | [diff] [blame] | 346 | this.newValue = newValue; |
| 347 | } |
| 348 | |
| 349 | /** Returns the value that this node changed to. */ |
| 350 | public SkyValue getNewValue() { |
| 351 | return newValue; |
Googler | 0eedeba | 2018-10-31 14:27:31 -0700 | [diff] [blame] | 352 | } |
| 353 | } |
| 354 | |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 355 | @Override |
| 356 | public synchronized DependencyState addReverseDepAndCheckIfDone(SkyKey reverseDep) { |
ulfjack | 9beabe0 | 2019-02-13 23:41:59 -0800 | [diff] [blame] | 357 | boolean done = isDone(); |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 358 | if (reverseDep != null) { |
ulfjack | 9beabe0 | 2019-02-13 23:41:59 -0800 | [diff] [blame] | 359 | if (done) { |
janakr | 1cde872 | 2017-10-10 03:22:21 +0200 | [diff] [blame] | 360 | if (keepReverseDeps()) { |
Janak Ramakrishnan | cb8a97d | 2017-03-23 20:50:08 +0000 | [diff] [blame] | 361 | ReverseDepsUtility.addReverseDeps(this, ImmutableList.of(reverseDep)); |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 362 | } |
| 363 | } else { |
Janak Ramakrishnan | cb8a97d | 2017-03-23 20:50:08 +0000 | [diff] [blame] | 364 | appendToReverseDepOperations(reverseDep, Op.ADD); |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 365 | } |
| 366 | } |
ulfjack | 9beabe0 | 2019-02-13 23:41:59 -0800 | [diff] [blame] | 367 | if (done) { |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 368 | return DependencyState.DONE; |
| 369 | } |
ulfjack | 9beabe0 | 2019-02-13 23:41:59 -0800 | [diff] [blame] | 370 | if (dirtyBuildingState == null) { |
| 371 | dirtyBuildingState = DirtyBuildingState.createNew(); |
| 372 | } |
mschaller | ac6d4fd | 2019-02-27 14:01:44 -0800 | [diff] [blame] | 373 | boolean wasEvaluating = dirtyBuildingState.isEvaluating(); |
| 374 | if (!wasEvaluating) { |
ulfjack | 9beabe0 | 2019-02-13 23:41:59 -0800 | [diff] [blame] | 375 | dirtyBuildingState.startEvaluating(); |
Janak Ramakrishnan | ab10f47 | 2017-03-24 17:08:24 +0000 | [diff] [blame] | 376 | } |
mschaller | ac6d4fd | 2019-02-27 14:01:44 -0800 | [diff] [blame] | 377 | return wasEvaluating ? DependencyState.ALREADY_EVALUATING : DependencyState.NEEDS_SCHEDULING; |
Janak Ramakrishnan | 5877b8b | 2015-09-22 17:37:10 +0000 | [diff] [blame] | 378 | } |
| 379 | |
Janak Ramakrishnan | cb8a97d | 2017-03-23 20:50:08 +0000 | [diff] [blame] | 380 | /** Sets {@link #reverseDeps}. Does not alter {@link #reverseDepsDataToConsolidate}. */ |
| 381 | synchronized void setSingleReverseDepForReverseDepsUtil(SkyKey reverseDep) { |
| 382 | this.reverseDeps = reverseDep; |
| 383 | } |
| 384 | |
| 385 | /** Sets {@link #reverseDeps}. Does not alter {@link #reverseDepsDataToConsolidate}. */ |
| 386 | synchronized void setReverseDepsForReverseDepsUtil(List<SkyKey> reverseDeps) { |
| 387 | this.reverseDeps = reverseDeps; |
| 388 | } |
| 389 | |
| 390 | /** Sets {@link #reverseDepsDataToConsolidate}. Does not alter {@link #reverseDeps}. */ |
| 391 | synchronized void setReverseDepsDataToConsolidateForReverseDepsUtil( |
| 392 | List<Object> dataToConsolidate) { |
| 393 | this.reverseDepsDataToConsolidate = dataToConsolidate; |
| 394 | } |
| 395 | |
| 396 | synchronized Object getReverseDepsRawForReverseDepsUtil() { |
| 397 | return this.reverseDeps; |
| 398 | } |
| 399 | |
| 400 | synchronized List<Object> getReverseDepsDataToConsolidateForReverseDepsUtil() { |
| 401 | return this.reverseDepsDataToConsolidate; |
| 402 | } |
| 403 | |
| 404 | private synchronized void appendToReverseDepOperations(SkyKey reverseDep, Op op) { |
| 405 | Preconditions.checkState(!isDone(), "Don't append to done %s %s %s", this, reverseDep, op); |
| 406 | if (reverseDepsDataToConsolidate == null) { |
| 407 | reverseDepsDataToConsolidate = new ArrayList<>(); |
| 408 | } |
| 409 | Preconditions.checkState( |
| 410 | isDirty() || op != Op.CHECK, "Not dirty check %s %s", this, reverseDep); |
| 411 | reverseDepsDataToConsolidate.add(KeyToConsolidate.create(reverseDep, op, getOpToStoreBare())); |
| 412 | } |
| 413 | |
ulfjack | 9beabe0 | 2019-02-13 23:41:59 -0800 | [diff] [blame] | 414 | /** |
| 415 | * In order to reduce memory consumption, we want to store reverse deps 'bare', i.e., without |
| 416 | * wrapping them in a KeyToConsolidate object. To that end, we define a bare op that is used for |
| 417 | * both storing and retrieving the deps. This method returns said op, and may adjust it depending |
| 418 | * on whether this is a new node entry (where all deps must be new) or an existing node entry |
| 419 | * (which most likely checks deps rather than adding new deps). |
| 420 | */ |
janakr | 3b5b55b | 2017-11-21 07:35:05 -0800 | [diff] [blame] | 421 | protected OpToStoreBare getOpToStoreBare() { |
ulfjack | 9beabe0 | 2019-02-13 23:41:59 -0800 | [diff] [blame] | 422 | return isDirty() && dirtyBuildingState.isDirty() ? OpToStoreBare.CHECK : OpToStoreBare.ADD; |
Janak Ramakrishnan | cb8a97d | 2017-03-23 20:50:08 +0000 | [diff] [blame] | 423 | } |
| 424 | |
Janak Ramakrishnan | 5877b8b | 2015-09-22 17:37:10 +0000 | [diff] [blame] | 425 | @Override |
| 426 | public synchronized DependencyState checkIfDoneForDirtyReverseDep(SkyKey reverseDep) { |
| 427 | Preconditions.checkNotNull(reverseDep, this); |
janakr | 1cde872 | 2017-10-10 03:22:21 +0200 | [diff] [blame] | 428 | // Note that implementations of InMemoryNodeEntry that have |
| 429 | // #keepEdges == KeepEdgesPolicy.JUST_DEPS may override this entire method. |
| 430 | Preconditions.checkState( |
| 431 | keepEdges() == KeepEdgesPolicy.ALL, |
| 432 | "Incremental means keeping edges %s %s", |
| 433 | reverseDep, |
| 434 | this); |
Janak Ramakrishnan | cb8a97d | 2017-03-23 20:50:08 +0000 | [diff] [blame] | 435 | if (isDone()) { |
| 436 | ReverseDepsUtility.checkReverseDep(this, reverseDep); |
Janak Ramakrishnan | 5877b8b | 2015-09-22 17:37:10 +0000 | [diff] [blame] | 437 | } else { |
Janak Ramakrishnan | cb8a97d | 2017-03-23 20:50:08 +0000 | [diff] [blame] | 438 | appendToReverseDepOperations(reverseDep, Op.CHECK); |
Janak Ramakrishnan | 5877b8b | 2015-09-22 17:37:10 +0000 | [diff] [blame] | 439 | } |
| 440 | return addReverseDepAndCheckIfDone(null); |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | @Override |
| 444 | public synchronized void removeReverseDep(SkyKey reverseDep) { |
janakr | 1cde872 | 2017-10-10 03:22:21 +0200 | [diff] [blame] | 445 | if (!keepReverseDeps()) { |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 446 | return; |
| 447 | } |
Janak Ramakrishnan | cb8a97d | 2017-03-23 20:50:08 +0000 | [diff] [blame] | 448 | if (isDone()) { |
| 449 | ReverseDepsUtility.removeReverseDep(this, reverseDep); |
| 450 | } else { |
| 451 | // Removing a reverse dep from an in-flight node is rare -- it should only happen when this |
| 452 | // node is about to be cleaned from the graph. |
| 453 | appendToReverseDepOperations(reverseDep, Op.REMOVE_OLD); |
| 454 | } |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | @Override |
Janak Ramakrishnan | 5877b8b | 2015-09-22 17:37:10 +0000 | [diff] [blame] | 458 | public synchronized void removeInProgressReverseDep(SkyKey reverseDep) { |
Janak Ramakrishnan | cb8a97d | 2017-03-23 20:50:08 +0000 | [diff] [blame] | 459 | appendToReverseDepOperations(reverseDep, Op.REMOVE); |
Janak Ramakrishnan | 5877b8b | 2015-09-22 17:37:10 +0000 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | @Override |
shreyax | 446f0ba | 2017-09-19 21:08:08 +0200 | [diff] [blame] | 463 | public synchronized Set<SkyKey> getReverseDepsForDoneEntry() { |
janakr | 1cde872 | 2017-10-10 03:22:21 +0200 | [diff] [blame] | 464 | assertKeepRdeps(); |
Janak Ramakrishnan | cb8a97d | 2017-03-23 20:50:08 +0000 | [diff] [blame] | 465 | Preconditions.checkState(isDone(), "Called on not done %s", this); |
| 466 | return ReverseDepsUtility.getReverseDeps(this); |
| 467 | } |
| 468 | |
| 469 | @Override |
shreyax | 446f0ba | 2017-09-19 21:08:08 +0200 | [diff] [blame] | 470 | public synchronized Set<SkyKey> getAllReverseDepsForNodeBeingDeleted() { |
janakr | 1cde872 | 2017-10-10 03:22:21 +0200 | [diff] [blame] | 471 | assertKeepRdeps(); |
Janak Ramakrishnan | cb8a97d | 2017-03-23 20:50:08 +0000 | [diff] [blame] | 472 | if (!isDone()) { |
| 473 | // This consolidation loses information about pending reverse deps to signal, but that is |
| 474 | // unimportant since this node is being deleted. |
| 475 | ReverseDepsUtility.consolidateDataAndReturnNewElements(this, getOpToStoreBare()); |
Janak Ramakrishnan | 5877b8b | 2015-09-22 17:37:10 +0000 | [diff] [blame] | 476 | } |
Janak Ramakrishnan | cb8a97d | 2017-03-23 20:50:08 +0000 | [diff] [blame] | 477 | return ReverseDepsUtility.getReverseDeps(this); |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | @Override |
shahan | 44666dc | 2018-10-05 17:47:11 -0700 | [diff] [blame] | 481 | public synchronized boolean signalDep(Version childVersion, @Nullable SkyKey childForDebugging) { |
| 482 | Preconditions.checkState( |
| 483 | !isDone(), "Value must not be done in signalDep %s child=%s", this, childForDebugging); |
ulfjack | 9beabe0 | 2019-02-13 23:41:59 -0800 | [diff] [blame] | 484 | Preconditions.checkNotNull(dirtyBuildingState, "%s %s", this, childForDebugging); |
| 485 | Preconditions.checkState(dirtyBuildingState.isEvaluating(), "%s %s", this, childForDebugging); |
| 486 | dirtyBuildingState.signalDep(); |
| 487 | dirtyBuildingState.signalDepPostProcess( |
| 488 | childCausesReevaluation(lastEvaluatedVersion, childVersion, childForDebugging), |
| 489 | getNumTemporaryDirectDeps()); |
Janak Ramakrishnan | ab10f47 | 2017-03-24 17:08:24 +0000 | [diff] [blame] | 490 | return isReady(); |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 491 | } |
| 492 | |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 493 | /** Checks that a caller is not trying to access not-stored graph edges. */ |
janakr | 1cde872 | 2017-10-10 03:22:21 +0200 | [diff] [blame] | 494 | private void assertKeepDeps() { |
| 495 | Preconditions.checkState(keepEdges() != KeepEdgesPolicy.NONE, "Not keeping deps: %s", this); |
| 496 | } |
| 497 | |
| 498 | /** Checks that a caller is not trying to access not-stored graph edges. */ |
| 499 | private void assertKeepRdeps() { |
| 500 | Preconditions.checkState(keepEdges() == KeepEdgesPolicy.ALL, "Not keeping rdeps: %s", this); |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | @Override |
mschaller | 7138b07 | 2018-09-28 10:14:11 -0700 | [diff] [blame] | 504 | public synchronized MarkedDirtyResult markDirty(DirtyType dirtyType) { |
janakr | 1cde872 | 2017-10-10 03:22:21 +0200 | [diff] [blame] | 505 | // Can't process a dirty node without its deps. |
| 506 | assertKeepDeps(); |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 507 | if (isDone()) { |
Janak Ramakrishnan | ab10f47 | 2017-03-24 17:08:24 +0000 | [diff] [blame] | 508 | dirtyBuildingState = |
Googler | baefeab | 2019-04-30 17:12:55 -0700 | [diff] [blame] | 509 | DirtyBuildingState.create( |
| 510 | dirtyType, GroupedList.create(getCompressedDirectDepsForDoneEntry()), value); |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 511 | value = null; |
Janak Ramakrishnan | 5093499 | 2016-07-06 17:09:51 +0000 | [diff] [blame] | 512 | directDeps = null; |
mschaller | 7138b07 | 2018-09-28 10:14:11 -0700 | [diff] [blame] | 513 | return new MarkedDirtyResult(ReverseDepsUtility.getReverseDeps(this)); |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 514 | } |
mschaller | 7138b07 | 2018-09-28 10:14:11 -0700 | [diff] [blame] | 515 | if (dirtyType.equals(DirtyType.FORCE_REBUILD)) { |
mschaller | 0d7c71a | 2019-03-05 08:50:21 -0800 | [diff] [blame] | 516 | if (dirtyBuildingState != null) { |
| 517 | dirtyBuildingState.markForceRebuild(); |
| 518 | } |
mschaller | 7138b07 | 2018-09-28 10:14:11 -0700 | [diff] [blame] | 519 | return null; |
| 520 | } |
| 521 | // The caller may be simultaneously trying to mark this node dirty and changed, and the dirty |
| 522 | // thread may have lost the race, but it is the caller's responsibility not to try to mark |
| 523 | // this node changed twice. The end result of racing markers must be a changed node, since one |
| 524 | // of the markers is trying to mark the node changed. |
| 525 | Preconditions.checkState( |
| 526 | dirtyType.equals(DirtyType.CHANGE) != isChanged(), |
| 527 | "Cannot mark node dirty twice or changed twice: %s", |
| 528 | this); |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 529 | Preconditions.checkState(value == null, "Value should have been reset already %s", this); |
mschaller | 7138b07 | 2018-09-28 10:14:11 -0700 | [diff] [blame] | 530 | if (dirtyType.equals(DirtyType.CHANGE)) { |
ulfjack | 9beabe0 | 2019-02-13 23:41:59 -0800 | [diff] [blame] | 531 | Preconditions.checkNotNull(dirtyBuildingState); |
mschaller | 7138b07 | 2018-09-28 10:14:11 -0700 | [diff] [blame] | 532 | // If the changed marker lost the race, we just need to mark changed in this method -- all |
| 533 | // other work was done by the dirty marker. |
ulfjack | 9beabe0 | 2019-02-13 23:41:59 -0800 | [diff] [blame] | 534 | dirtyBuildingState.markChanged(); |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 535 | } |
mschaller | 7138b07 | 2018-09-28 10:14:11 -0700 | [diff] [blame] | 536 | return null; |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | @Override |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 540 | public synchronized Set<SkyKey> markClean() throws InterruptedException { |
ulfjack | 9beabe0 | 2019-02-13 23:41:59 -0800 | [diff] [blame] | 541 | Preconditions.checkNotNull(dirtyBuildingState, this); |
| 542 | this.value = Preconditions.checkNotNull(dirtyBuildingState.getLastBuildValue()); |
Janak Ramakrishnan | 5093499 | 2016-07-06 17:09:51 +0000 | [diff] [blame] | 543 | Preconditions.checkState(isReady(), "Should be ready when clean: %s", this); |
| 544 | Preconditions.checkState( |
ulfjack | 9beabe0 | 2019-02-13 23:41:59 -0800 | [diff] [blame] | 545 | dirtyBuildingState.depsUnchangedFromLastBuild(getTemporaryDirectDeps()), |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 546 | "Direct deps must be the same as those found last build for node to be marked clean: %s", |
| 547 | this); |
| 548 | Preconditions.checkState(isDirty(), this); |
Janak Ramakrishnan | ab10f47 | 2017-03-24 17:08:24 +0000 | [diff] [blame] | 549 | Preconditions.checkState(!dirtyBuildingState.isChanged(), "shouldn't be changed: %s", this); |
Janak Ramakrishnan | 772b5bb | 2016-06-29 00:20:36 +0000 | [diff] [blame] | 550 | return setStateFinishedAndReturnReverseDepsToSignal(); |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | @Override |
| 554 | public synchronized void forceRebuild() { |
ulfjack | 9beabe0 | 2019-02-13 23:41:59 -0800 | [diff] [blame] | 555 | Preconditions.checkNotNull(dirtyBuildingState, this); |
| 556 | Preconditions.checkState(isEvaluating(), this); |
| 557 | dirtyBuildingState.forceRebuild(getNumTemporaryDirectDeps()); |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | @Override |
Googler | c5a2f81 | 2018-08-21 14:08:59 -0700 | [diff] [blame] | 561 | public Version getVersion() { |
Janak Ramakrishnan | 1d22d4c | 2015-11-18 19:28:45 +0000 | [diff] [blame] | 562 | return lastChangedVersion; |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 563 | } |
| 564 | |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 565 | @Override |
| 566 | public synchronized NodeEntry.DirtyState getDirtyState() { |
ulfjack | 9beabe0 | 2019-02-13 23:41:59 -0800 | [diff] [blame] | 567 | Preconditions.checkNotNull(dirtyBuildingState, this); |
| 568 | return dirtyBuildingState.getDirtyState(); |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 569 | } |
| 570 | |
Janak Ramakrishnan | 5dddb00 | 2016-07-06 20:07:23 +0000 | [diff] [blame] | 571 | /** @see DirtyBuildingState#getNextDirtyDirectDeps() */ |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 572 | @Override |
janakr | ad1c2e4 | 2019-02-08 14:00:03 -0800 | [diff] [blame] | 573 | public synchronized List<SkyKey> getNextDirtyDirectDeps() throws InterruptedException { |
Janak Ramakrishnan | 5877b8b | 2015-09-22 17:37:10 +0000 | [diff] [blame] | 574 | Preconditions.checkState(isReady(), this); |
ulfjack | 9beabe0 | 2019-02-13 23:41:59 -0800 | [diff] [blame] | 575 | Preconditions.checkNotNull(dirtyBuildingState, this); |
| 576 | Preconditions.checkState( |
| 577 | dirtyBuildingState.isEvaluating(), "Not evaluating during getNextDirty? %s", this); |
| 578 | return dirtyBuildingState.getNextDirtyDirectDeps(); |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | @Override |
Googler | 8d2311d | 2017-01-31 22:52:26 +0000 | [diff] [blame] | 582 | public synchronized Iterable<SkyKey> getAllDirectDepsForIncompleteNode() |
| 583 | throws InterruptedException { |
Janak Ramakrishnan | 5877b8b | 2015-09-22 17:37:10 +0000 | [diff] [blame] | 584 | Preconditions.checkState(!isDone(), this); |
| 585 | if (!isDirty()) { |
shreyax | a6679ae | 2018-03-02 15:59:46 -0800 | [diff] [blame] | 586 | return getTemporaryDirectDeps().getAllElementsAsIterable(); |
Janak Ramakrishnan | 5877b8b | 2015-09-22 17:37:10 +0000 | [diff] [blame] | 587 | } else { |
Janak Ramakrishnan | f76c959 | 2016-05-17 21:42:50 +0000 | [diff] [blame] | 588 | // There may be duplicates here. Make sure everything is unique. |
| 589 | ImmutableSet.Builder<SkyKey> result = ImmutableSet.builder(); |
| 590 | for (Iterable<SkyKey> group : getTemporaryDirectDeps()) { |
| 591 | result.addAll(group); |
| 592 | } |
ulfjack | 9beabe0 | 2019-02-13 23:41:59 -0800 | [diff] [blame] | 593 | result.addAll(dirtyBuildingState.getAllRemainingDirtyDirectDeps(/*preservePosition=*/ false)); |
Janak Ramakrishnan | f76c959 | 2016-05-17 21:42:50 +0000 | [diff] [blame] | 594 | return result.build(); |
Janak Ramakrishnan | 5877b8b | 2015-09-22 17:37:10 +0000 | [diff] [blame] | 595 | } |
| 596 | } |
| 597 | |
| 598 | @Override |
janakr | 3cb0c39 | 2019-03-29 20:08:41 -0700 | [diff] [blame] | 599 | public synchronized ImmutableSet<SkyKey> getAllRemainingDirtyDirectDeps() |
| 600 | throws InterruptedException { |
ulfjack | 9beabe0 | 2019-02-13 23:41:59 -0800 | [diff] [blame] | 601 | Preconditions.checkNotNull(dirtyBuildingState, this); |
| 602 | Preconditions.checkState( |
| 603 | dirtyBuildingState.isEvaluating(), "Not evaluating for remaining dirty? %s", this); |
Janak Ramakrishnan | f76c959 | 2016-05-17 21:42:50 +0000 | [diff] [blame] | 604 | if (isDirty()) { |
ulfjack | 9beabe0 | 2019-02-13 23:41:59 -0800 | [diff] [blame] | 605 | DirtyState dirtyState = dirtyBuildingState.getDirtyState(); |
Janak Ramakrishnan | 5dddb00 | 2016-07-06 20:07:23 +0000 | [diff] [blame] | 606 | Preconditions.checkState( |
janakr | e54491e | 2018-07-11 16:29:13 -0700 | [diff] [blame] | 607 | dirtyState == DirtyState.REBUILDING || dirtyState == DirtyState.FORCED_REBUILDING, this); |
ulfjack | 9beabe0 | 2019-02-13 23:41:59 -0800 | [diff] [blame] | 608 | return dirtyBuildingState.getAllRemainingDirtyDirectDeps(/*preservePosition=*/ true); |
Janak Ramakrishnan | f76c959 | 2016-05-17 21:42:50 +0000 | [diff] [blame] | 609 | } else { |
Janak Ramakrishnan | f76c959 | 2016-05-17 21:42:50 +0000 | [diff] [blame] | 610 | return ImmutableSet.of(); |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | @Override |
janakr | a2d4d3d | 2018-12-10 18:30:08 -0800 | [diff] [blame] | 615 | public boolean canPruneDepsByFingerprint() { |
| 616 | return false; |
| 617 | } |
| 618 | |
| 619 | @Nullable |
| 620 | @Override |
| 621 | public Iterable<SkyKey> getLastDirectDepsGroupWhenPruningDepsByFingerprint() |
| 622 | throws InterruptedException { |
| 623 | throw new UnsupportedOperationException(this.toString()); |
| 624 | } |
| 625 | |
| 626 | @Override |
| 627 | public boolean unmarkNeedsRebuildingIfGroupUnchangedUsingFingerprint( |
| 628 | BigInteger groupFingerprint) { |
| 629 | throw new UnsupportedOperationException(this.toString()); |
| 630 | } |
| 631 | |
| 632 | /** |
| 633 | * If this entry {@link #canPruneDepsByFingerprint} and has that data, returns a list of dep group |
| 634 | * fingerprints. Otherwise returns null. |
| 635 | */ |
| 636 | @Nullable |
| 637 | public DepFingerprintList getDepFingerprintList() { |
| 638 | Preconditions.checkState(isDone(), this); |
| 639 | return null; |
| 640 | } |
| 641 | |
| 642 | @Override |
Janak Ramakrishnan | f76c959 | 2016-05-17 21:42:50 +0000 | [diff] [blame] | 643 | public synchronized void markRebuilding() { |
ulfjack | 9beabe0 | 2019-02-13 23:41:59 -0800 | [diff] [blame] | 644 | Preconditions.checkNotNull(dirtyBuildingState, this); |
| 645 | dirtyBuildingState.markRebuilding(isEligibleForChangePruningOnUnchangedValue()); |
Janak Ramakrishnan | 5877b8b | 2015-09-22 17:37:10 +0000 | [diff] [blame] | 646 | } |
| 647 | |
Janak Ramakrishnan | 5093499 | 2016-07-06 17:09:51 +0000 | [diff] [blame] | 648 | @SuppressWarnings("unchecked") |
Janak Ramakrishnan | 5877b8b | 2015-09-22 17:37:10 +0000 | [diff] [blame] | 649 | @Override |
Janak Ramakrishnan | 3b5d5d2 | 2016-05-13 21:14:56 +0000 | [diff] [blame] | 650 | public synchronized GroupedList<SkyKey> getTemporaryDirectDeps() { |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 651 | Preconditions.checkState(!isDone(), "temporary shouldn't be done: %s", this); |
Janak Ramakrishnan | 5093499 | 2016-07-06 17:09:51 +0000 | [diff] [blame] | 652 | if (directDeps == null) { |
| 653 | // Initialize lazily, to save a little bit of memory. |
| 654 | directDeps = new GroupedList<SkyKey>(); |
| 655 | } |
| 656 | return (GroupedList<SkyKey>) directDeps; |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 657 | } |
| 658 | |
felly | 44eb964 | 2018-03-27 12:41:13 -0700 | [diff] [blame] | 659 | private synchronized int getNumTemporaryDirectDeps() { |
| 660 | return directDeps == null ? 0 : getTemporaryDirectDeps().numElements(); |
| 661 | } |
| 662 | |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 663 | @Override |
| 664 | public synchronized boolean noDepsLastBuild() { |
ulfjack | 9beabe0 | 2019-02-13 23:41:59 -0800 | [diff] [blame] | 665 | Preconditions.checkState(isEvaluating(), this); |
| 666 | return dirtyBuildingState.noDepsLastBuild(); |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 667 | } |
| 668 | |
Janak Ramakrishnan | 5093499 | 2016-07-06 17:09:51 +0000 | [diff] [blame] | 669 | /** |
| 670 | * {@inheritDoc} |
| 671 | * |
| 672 | * <p>This is complicated by the need to maintain the group data. If we remove a dep that ended a |
| 673 | * group, then its predecessor's group data must be changed to indicate that it now ends the |
| 674 | * group. |
| 675 | */ |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 676 | @Override |
| 677 | public synchronized void removeUnfinishedDeps(Set<SkyKey> unfinishedDeps) { |
Janak Ramakrishnan | 5093499 | 2016-07-06 17:09:51 +0000 | [diff] [blame] | 678 | getTemporaryDirectDeps().remove(unfinishedDeps); |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | @Override |
janakr | 46706ae | 2018-04-30 16:18:50 -0700 | [diff] [blame] | 682 | public synchronized void resetForRestartFromScratch() { |
mschaller | af4493c | 2019-05-08 12:04:47 -0700 | [diff] [blame] | 683 | Preconditions.checkState(isReady(), this); |
janakr | 46706ae | 2018-04-30 16:18:50 -0700 | [diff] [blame] | 684 | directDeps = null; |
ulfjack | 9beabe0 | 2019-02-13 23:41:59 -0800 | [diff] [blame] | 685 | dirtyBuildingState.resetForRestartFromScratch(); |
janakr | 46706ae | 2018-04-30 16:18:50 -0700 | [diff] [blame] | 686 | } |
| 687 | |
| 688 | @Override |
Janak Ramakrishnan | 2b8a3a4 | 2016-10-14 11:41:27 +0000 | [diff] [blame] | 689 | public synchronized Set<SkyKey> addTemporaryDirectDeps(GroupedListHelper<SkyKey> helper) { |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 690 | Preconditions.checkState(!isDone(), "add temp shouldn't be done: %s %s", helper, this); |
Janak Ramakrishnan | 2b8a3a4 | 2016-10-14 11:41:27 +0000 | [diff] [blame] | 691 | return getTemporaryDirectDeps().append(helper); |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | @Override |
janakr | ad1c2e4 | 2019-02-08 14:00:03 -0800 | [diff] [blame] | 695 | public synchronized void addTemporaryDirectDepsGroupToDirtyEntry(List<SkyKey> group) { |
Mark Schaller | d1cd14b | 2015-12-09 16:23:22 +0000 | [diff] [blame] | 696 | Preconditions.checkState(!isDone(), "add group temp shouldn't be done: %s %s", group, this); |
Janak Ramakrishnan | 5093499 | 2016-07-06 17:09:51 +0000 | [diff] [blame] | 697 | getTemporaryDirectDeps().appendGroup(group); |
Mark Schaller | d1cd14b | 2015-12-09 16:23:22 +0000 | [diff] [blame] | 698 | } |
| 699 | |
janakr | 8d98d92 | 2018-10-03 15:55:28 -0700 | [diff] [blame] | 700 | /** True if the child should cause re-evaluation of this node. */ |
shahan | 44666dc | 2018-10-05 17:47:11 -0700 | [diff] [blame] | 701 | protected boolean childCausesReevaluation( |
| 702 | Version lastEvaluatedVersion, |
| 703 | Version childVersion, |
| 704 | @Nullable SkyKey unusedChildForDebugging) { |
shahan | d4a6ca0 | 2018-10-03 11:41:28 -0700 | [diff] [blame] | 705 | // childVersion > lastEvaluatedVersion |
| 706 | return !childVersion.atMost(lastEvaluatedVersion); |
| 707 | } |
| 708 | |
shahan | d4a6ca0 | 2018-10-03 11:41:28 -0700 | [diff] [blame] | 709 | protected void logError(RuntimeException error) { |
| 710 | throw error; |
| 711 | } |
| 712 | |
janakr | a2d4d3d | 2018-12-10 18:30:08 -0800 | [diff] [blame] | 713 | protected synchronized MoreObjects.ToStringHelper toStringHelper() { |
Googler | 2c19a57 | 2015-07-16 08:38:49 +0000 | [diff] [blame] | 714 | return MoreObjects.toStringHelper(this) |
Janak Ramakrishnan | 5877b8b | 2015-09-22 17:37:10 +0000 | [diff] [blame] | 715 | .add("identity", System.identityHashCode(this)) |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 716 | .add("value", value) |
Janak Ramakrishnan | 1d22d4c | 2015-11-18 19:28:45 +0000 | [diff] [blame] | 717 | .add("lastChangedVersion", lastChangedVersion) |
| 718 | .add("lastEvaluatedVersion", lastEvaluatedVersion) |
janakr | dd4c47f | 2019-02-08 17:03:19 -0800 | [diff] [blame] | 719 | .add( |
| 720 | "directDeps", |
| 721 | isDone() && keepEdges() != KeepEdgesPolicy.NONE |
Googler | baefeab | 2019-04-30 17:12:55 -0700 | [diff] [blame] | 722 | ? GroupedList.create(getCompressedDirectDepsForDoneEntry()) |
janakr | dd4c47f | 2019-02-08 17:03:19 -0800 | [diff] [blame] | 723 | : directDeps) |
Janak Ramakrishnan | cb8a97d | 2017-03-23 20:50:08 +0000 | [diff] [blame] | 724 | .add("reverseDeps", ReverseDepsUtility.toString(this)) |
janakr | a2d4d3d | 2018-12-10 18:30:08 -0800 | [diff] [blame] | 725 | .add("dirtyBuildingState", dirtyBuildingState); |
| 726 | } |
| 727 | |
| 728 | @Override |
| 729 | public final synchronized String toString() { |
| 730 | return toStringHelper().toString(); |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 731 | } |
| 732 | |
janakr | 1cde872 | 2017-10-10 03:22:21 +0200 | [diff] [blame] | 733 | protected synchronized InMemoryNodeEntry cloneNodeEntry(InMemoryNodeEntry newEntry) { |
| 734 | // As this is temporary, for now let's limit to done nodes. |
| 735 | Preconditions.checkState(isDone(), "Only done nodes can be copied: %s", this); |
| 736 | newEntry.value = value; |
| 737 | newEntry.lastChangedVersion = this.lastChangedVersion; |
| 738 | newEntry.lastEvaluatedVersion = this.lastEvaluatedVersion; |
| 739 | ReverseDepsUtility.addReverseDeps(newEntry, ReverseDepsUtility.getReverseDeps(this)); |
| 740 | newEntry.directDeps = directDeps; |
| 741 | newEntry.dirtyBuildingState = null; |
| 742 | return newEntry; |
| 743 | } |
| 744 | |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 745 | /** |
| 746 | * Do not use except in custom evaluator implementations! Added only temporarily. |
| 747 | * |
| 748 | * <p>Clones a InMemoryMutableNodeEntry iff it is a done node. Otherwise it fails. |
| 749 | */ |
| 750 | public synchronized InMemoryNodeEntry cloneNodeEntry() { |
janakr | 1cde872 | 2017-10-10 03:22:21 +0200 | [diff] [blame] | 751 | return cloneNodeEntry(new InMemoryNodeEntry()); |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 752 | } |
| 753 | } |