blob: 56f89978aece5f3b7f718df590e3e124ae14a6ba [file] [log] [blame]
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001// 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.
14package com.google.devtools.build.skyframe;
15
Nathan Harmatab408f9e2015-02-10 02:13:05 +000016import com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010017import com.google.devtools.build.lib.util.GroupedList.GroupedListHelper;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010018
19import java.util.Collection;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010020import java.util.Set;
21
22import javax.annotation.Nullable;
23
24/**
Nathan Harmatab408f9e2015-02-10 02:13:05 +000025 * A node in the graph. All operations on this class are thread-safe.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010026 *
Nathan Harmatab408f9e2015-02-10 02:13:05 +000027 * <p>This interface is public only for the benefit of alternative graph implementations outside of
28 * the package.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010029 */
Mark Schaller226ce6812015-09-01 22:44:58 +000030public interface NodeEntry extends ThinNodeEntry {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010031 /**
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 Nienhuysd08b27f2015-02-25 16:45:20 +010051 /**
Nathan Harmatab408f9e2015-02-10 02:13:05 +000052 * Return code for {@link #getDirtyState}.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010053 */
Nathan Harmatab408f9e2015-02-10 02:13:05 +000054 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 Nienhuysd08b27f2015-02-25 16:45:20 +010070 }
71
Nathan Harmatab408f9e2015-02-10 02:13:05 +000072 boolean keepEdges();
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010073
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010074 /**
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 Harmatab408f9e2015-02-10 02:13:05 +000078 @ThreadSafe
79 SkyValue getValue();
Janak Ramakrishnane72d5222015-02-26 17:09:18 +000080
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010081
82 /**
Janak Ramakrishnan0afd4532015-08-24 17:27:48 +000083 * 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 Nienhuysd08b27f2015-02-25 16:45:20 +010088 */
Nathan Harmatab408f9e2015-02-10 02:13:05 +000089 @ThreadSafe
Janak Ramakrishnan0afd4532015-08-24 17:27:48 +000090 SkyValue getValueMaybeWithMetadata();
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010091
92 /**
93 * Returns the value, even if dirty or changed. Returns null otherwise.
94 */
Nathan Harmatab408f9e2015-02-10 02:13:05 +000095 @ThreadSafe
96 SkyValue toValue();
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010097
98 /**
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010099 * 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 Harmatab408f9e2015-02-10 02:13:05 +0000103 @ThreadSafe
104 ErrorInfo getErrorInfo();
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100105
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 Harmatab408f9e2015-02-10 02:13:05 +0000111 @ThreadSafe
112 Set<SkyKey> getInProgressReverseDeps();
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100113
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 Harmatab408f9e2015-02-10 02:13:05 +0000130 @ThreadSafe
131 Set<SkyKey> setValue(SkyValue value, Version version);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100132
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 Harmatab408f9e2015-02-10 02:13:05 +0000151 @ThreadSafe
152 DependencyState addReverseDepAndCheckIfDone(SkyKey reverseDep);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100153
154 /**
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100155 * 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 Harmatab408f9e2015-02-10 02:13:05 +0000161 @ThreadSafe
162 boolean signalDep();
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100163
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 Harmatab408f9e2015-02-10 02:13:05 +0000172 * {@link #getDirtyState()} will return {@link DirtyState#REBUILDING}.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100173 */
Nathan Harmatab408f9e2015-02-10 02:13:05 +0000174 @ThreadSafe
175 boolean signalDep(Version childVersion);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100176
177 /**
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100178 * 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 Harmatab408f9e2015-02-10 02:13:05 +0000182 @ThreadSafe
183 Set<SkyKey> markClean();
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100184
185 /**
Mark Schaller810b6962015-04-06 15:19:40 +0000186 * Forces this node to be re-evaluated, even if none of its dependencies are known to have
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100187 * 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 Harmatab408f9e2015-02-10 02:13:05 +0000193 @ThreadSafe
194 void forceRebuild();
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100195
196 /**
197 * Gets the current version of this entry.
198 */
Nathan Harmatab408f9e2015-02-10 02:13:05 +0000199 @ThreadSafe
200 Version getVersion();
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100201
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 Harmatab408f9e2015-02-10 02:13:05 +0000205 * as enumerated by {@link NodeEntry.DirtyState}.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100206 */
Nathan Harmatab408f9e2015-02-10 02:13:05 +0000207 @ThreadSafe
208 NodeEntry.DirtyState getDirtyState();
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100209
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 Harmatab408f9e2015-02-10 02:13:05 +0000214 * {@link DirtyState#CHECK_DEPENDENCIES} before calling this method -- any other
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100215 * 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 Harmatab408f9e2015-02-10 02:13:05 +0000229 @ThreadSafe
230 Collection<SkyKey> getNextDirtyDirectDeps();
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100231
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 Harmatab408f9e2015-02-10 02:13:05 +0000236 @ThreadSafe
237 Set<SkyKey> getTemporaryDirectDeps();
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100238
Nathan Harmatab408f9e2015-02-10 02:13:05 +0000239 @ThreadSafe
240 boolean noDepsLastBuild();
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100241
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 Harmatab408f9e2015-02-10 02:13:05 +0000249 @ThreadSafe
250 void removeUnfinishedDeps(Set<SkyKey> unfinishedDeps);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100251
Nathan Harmatab408f9e2015-02-10 02:13:05 +0000252 @ThreadSafe
253 void addTemporaryDirectDeps(GroupedListHelper<SkyKey> helper);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100254
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 Harmatab408f9e2015-02-10 02:13:05 +0000260 @ThreadSafe
261 boolean isReady();
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100262}