Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1 | // Copyright 2014 Google Inc. All rights reserved. |
| 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 | |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 16 | import com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 17 | import com.google.devtools.build.lib.util.GroupedList.GroupedListHelper; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 18 | |
| 19 | import java.util.Collection; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 20 | import java.util.Set; |
| 21 | |
| 22 | import javax.annotation.Nullable; |
| 23 | |
| 24 | /** |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 25 | * A node in the graph. All operations on this class are thread-safe. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 26 | * |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 27 | * <p>This interface is public only for the benefit of alternative graph implementations outside of |
| 28 | * the package. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 29 | */ |
Mark Schaller | 226ce681 | 2015-09-01 22:44:58 +0000 | [diff] [blame] | 30 | public interface NodeEntry extends ThinNodeEntry { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 31 | /** |
| 32 | * Return code for {@link #addReverseDepAndCheckIfDone(SkyKey)}. |
| 33 | */ |
| 34 | enum DependencyState { |
| 35 | /** The node is done. */ |
| 36 | DONE, |
| 37 | |
| 38 | /** |
| 39 | * The node was just created and needs to be scheduled for its first evaluation pass. The |
| 40 | * evaluator is responsible for signaling the reverse dependency node. |
| 41 | */ |
| 42 | NEEDS_SCHEDULING, |
| 43 | |
| 44 | /** |
| 45 | * The node was already created, but isn't done yet. The evaluator is responsible for |
| 46 | * signaling the reverse dependency node. |
| 47 | */ |
| 48 | ADDED_DEP; |
| 49 | } |
| 50 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 51 | /** |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 52 | * Return code for {@link #getDirtyState}. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 53 | */ |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 54 | enum DirtyState { |
| 55 | /** |
| 56 | * The node's dependencies need to be checked to see if it needs to be rebuilt. The |
| 57 | * dependencies must be obtained through calls to {@link #getNextDirtyDirectDeps} and checked. |
| 58 | */ |
| 59 | CHECK_DEPENDENCIES, |
| 60 | /** |
| 61 | * All of the node's dependencies are unchanged, and the value itself was not marked changed, |
| 62 | * so its current value is still valid -- it need not be rebuilt. |
| 63 | */ |
| 64 | VERIFIED_CLEAN, |
| 65 | /** |
| 66 | * A rebuilding is required or in progress, because either the node itself changed or one of |
| 67 | * its dependencies did. |
| 68 | */ |
| 69 | REBUILDING |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 70 | } |
| 71 | |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 72 | boolean keepEdges(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 73 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 74 | /** |
| 75 | * Returns the value stored in this entry. This method may only be called after the evaluation of |
| 76 | * this node is complete, i.e., after {@link #setValue} has been called. |
| 77 | */ |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 78 | @ThreadSafe |
| 79 | SkyValue getValue(); |
Janak Ramakrishnan | e72d522 | 2015-02-26 17:09:18 +0000 | [diff] [blame] | 80 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 81 | |
| 82 | /** |
Janak Ramakrishnan | 0afd453 | 2015-08-24 17:27:48 +0000 | [diff] [blame] | 83 | * Returns raw {@link SkyValue} stored in this entry, which may include metadata associated with |
| 84 | * it (like events and errors). This method may only be called after the evaluation of this node |
| 85 | * is complete, i.e., after {@link #setValue} has been called. |
| 86 | * |
| 87 | * <p>Use the static methods of {@link ValueWithMetadata} to extract metadata if necessary. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 88 | */ |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 89 | @ThreadSafe |
Janak Ramakrishnan | 0afd453 | 2015-08-24 17:27:48 +0000 | [diff] [blame] | 90 | SkyValue getValueMaybeWithMetadata(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 91 | |
| 92 | /** |
| 93 | * Returns the value, even if dirty or changed. Returns null otherwise. |
| 94 | */ |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 95 | @ThreadSafe |
| 96 | SkyValue toValue(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 97 | |
| 98 | /** |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 99 | * Returns the error, if any, associated to this node. This method may only be called after |
| 100 | * the evaluation of this node is complete, i.e., after {@link #setValue} has been called. |
| 101 | */ |
| 102 | @Nullable |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 103 | @ThreadSafe |
| 104 | ErrorInfo getErrorInfo(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 105 | |
| 106 | /** |
| 107 | * Returns the set of reverse deps that have been declared so far this build. Only for use in |
| 108 | * debugging and when bubbling errors up in the --nokeep_going case, where we need to know what |
| 109 | * parents this entry has. |
| 110 | */ |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 111 | @ThreadSafe |
| 112 | Set<SkyKey> getInProgressReverseDeps(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 113 | |
| 114 | /** |
| 115 | * Transitions the node from the EVALUATING to the DONE state and simultaneously sets it to the |
| 116 | * given value and error state. It then returns the set of reverse dependencies that need to be |
| 117 | * signaled. |
| 118 | * |
| 119 | * <p>This is an atomic operation to avoid a race where two threads work on two nodes, where one |
| 120 | * node depends on another (b depends on a). When a finishes, it signals <b>exactly</b> the set |
| 121 | * of reverse dependencies that are registered at the time of the {@code setValue} call. If b |
| 122 | * comes in before a, it is signaled (and re-scheduled) by a, otherwise it needs to do that |
| 123 | * itself. |
| 124 | * |
| 125 | * <p>{@code version} indicates the graph version at which this node is being written. If the |
| 126 | * entry determines that the new value is equal to the previous value, the entry will keep its |
| 127 | * current version. Callers can query that version to see if the node considers its value to have |
| 128 | * changed. |
| 129 | */ |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 130 | @ThreadSafe |
| 131 | Set<SkyKey> setValue(SkyValue value, Version version); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 132 | |
| 133 | /** |
| 134 | * Queries if the node is done and adds the given key as a reverse dependency. The return code |
| 135 | * indicates whether a) the node is done, b) the reverse dependency is the first one, so the |
| 136 | * node needs to be scheduled, or c) the reverse dependency was added, and the node does not |
| 137 | * need to be scheduled. |
| 138 | * |
| 139 | * <p>This method <b>must</b> be called before any processing of the entry. This encourages |
| 140 | * callers to check that the entry is ready to be processed. |
| 141 | * |
| 142 | * <p>Adding the dependency and checking if the node needs to be scheduled is an atomic operation |
| 143 | * to avoid a race where two threads work on two nodes, where one depends on the other (b depends |
| 144 | * on a). In that case, we need to ensure that b is re-scheduled exactly once when a is done. |
| 145 | * However, a may complete first, in which case b has to re-schedule itself. Also see {@link |
| 146 | * #setValue}. |
| 147 | * |
| 148 | * <p>If the parameter is {@code null}, then no reverse dependency is added, but we still check |
| 149 | * if the node needs to be scheduled. |
| 150 | */ |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 151 | @ThreadSafe |
| 152 | DependencyState addReverseDepAndCheckIfDone(SkyKey reverseDep); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 153 | |
| 154 | /** |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 155 | * Tell this node that one of its dependencies is now done. Callers must check the return value, |
| 156 | * and if true, they must re-schedule this node for evaluation. Equivalent to |
| 157 | * {@code #signalDep(Long.MAX_VALUE)}. Since this entry's version is less than |
| 158 | * {@link Long#MAX_VALUE}, informing this entry that a child of it has version |
| 159 | * {@link Long#MAX_VALUE} will force it to re-evaluate. |
| 160 | */ |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 161 | @ThreadSafe |
| 162 | boolean signalDep(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 163 | |
| 164 | /** |
| 165 | * Tell this entry that one of its dependencies is now done. Callers must check the return value, |
| 166 | * and if true, they must re-schedule this node for evaluation. |
| 167 | * |
| 168 | * @param childVersion If this entry {@link #isDirty()} and {@code childVersion} is not at most |
| 169 | * {@link #getVersion()}, then this entry records that one of its children has changed since it |
| 170 | * was last evaluated (namely, it was last evaluated at version {@link #getVersion()} and the |
| 171 | * child was last evaluated at {@code childVersion}. Thus, the next call to |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 172 | * {@link #getDirtyState()} will return {@link DirtyState#REBUILDING}. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 173 | */ |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 174 | @ThreadSafe |
| 175 | boolean signalDep(Version childVersion); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 176 | |
| 177 | /** |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 178 | * Marks this entry as up-to-date at this version. |
| 179 | * |
| 180 | * @return {@link Set} of reverse dependencies to signal that this node is done. |
| 181 | */ |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 182 | @ThreadSafe |
| 183 | Set<SkyKey> markClean(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 184 | |
| 185 | /** |
Mark Schaller | 810b696 | 2015-04-06 15:19:40 +0000 | [diff] [blame] | 186 | * Forces this node to be re-evaluated, even if none of its dependencies are known to have |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 187 | * changed. |
| 188 | * |
| 189 | * <p>Used when an external caller has reason to believe that re-evaluating may yield a new |
| 190 | * result. This method should not be used if one of the normal deps of this node has changed, |
| 191 | * the usual change-pruning process should work in that case. |
| 192 | */ |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 193 | @ThreadSafe |
| 194 | void forceRebuild(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 195 | |
| 196 | /** |
| 197 | * Gets the current version of this entry. |
| 198 | */ |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 199 | @ThreadSafe |
| 200 | Version getVersion(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 201 | |
| 202 | /** |
| 203 | * Gets the current state of checking this dirty entry to see if it must be re-evaluated. Must be |
| 204 | * called each time evaluation of a dirty entry starts to find the proper action to perform next, |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 205 | * as enumerated by {@link NodeEntry.DirtyState}. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 206 | */ |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 207 | @ThreadSafe |
| 208 | NodeEntry.DirtyState getDirtyState(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 209 | |
| 210 | /** |
| 211 | * Should only be called if the entry is dirty. During the examination to see if the entry must be |
| 212 | * re-evaluated, this method returns the next group of children to be checked. Callers should |
| 213 | * have already called {@link #getDirtyState} and received a return value of |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 214 | * {@link DirtyState#CHECK_DEPENDENCIES} before calling this method -- any other |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 215 | * return value from {@link #getDirtyState} means that this method must not be called, since |
| 216 | * whether or not the node needs to be rebuilt is already known. |
| 217 | * |
| 218 | * <p>Deps are returned in groups. The deps in each group were requested in parallel by the |
| 219 | * {@code SkyFunction} last build, meaning independently of the values of any other deps in this |
| 220 | * group (although possibly depending on deps in earlier groups). Thus the caller may check all |
| 221 | * the deps in this group in parallel, since the deps in all previous groups are verified |
| 222 | * unchanged. See {@link SkyFunction.Environment#getValues} for more on dependency groups. |
| 223 | * |
| 224 | * <p>The caller should register these as deps of this entry using {@link #addTemporaryDirectDeps} |
| 225 | * before checking them. |
| 226 | * |
| 227 | * @see BuildingState#getNextDirtyDirectDeps() |
| 228 | */ |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 229 | @ThreadSafe |
| 230 | Collection<SkyKey> getNextDirtyDirectDeps(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 231 | |
| 232 | /** |
| 233 | * Returns the set of direct dependencies. This may only be called while the node is being |
| 234 | * evaluated, that is, before {@link #setValue} and after {@link #markDirty}. |
| 235 | */ |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 236 | @ThreadSafe |
| 237 | Set<SkyKey> getTemporaryDirectDeps(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 238 | |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 239 | @ThreadSafe |
| 240 | boolean noDepsLastBuild(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 241 | |
| 242 | /** |
| 243 | * Remove dep from direct deps. This should only be called if this entry is about to be |
| 244 | * committed as a cycle node, but some of its children were not checked for cycles, either |
| 245 | * because the cycle was discovered before some children were checked; some children didn't have a |
| 246 | * chance to finish before the evaluator aborted; or too many cycles were found when it came time |
| 247 | * to check the children. |
| 248 | */ |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 249 | @ThreadSafe |
| 250 | void removeUnfinishedDeps(Set<SkyKey> unfinishedDeps); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 251 | |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 252 | @ThreadSafe |
| 253 | void addTemporaryDirectDeps(GroupedListHelper<SkyKey> helper); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 254 | |
| 255 | /** |
| 256 | * Returns true if the node is ready to be evaluated, i.e., it has been signaled exactly as many |
| 257 | * times as it has temporary dependencies. This may only be called while the node is being |
| 258 | * evaluated, that is, before {@link #setValue} and after {@link #markDirty}. |
| 259 | */ |
Nathan Harmata | b408f9e | 2015-02-10 02:13:05 +0000 | [diff] [blame] | 260 | @ThreadSafe |
| 261 | boolean isReady(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 262 | } |