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