blob: a55bbc54156b7b14a299b9cad2809898216a969c [file] [log] [blame]
Damien Martin-Guillerezf88f4d82015-09-25 13:56:55 +00001// Copyright 2015 The Bazel Authors. All rights reserved.
Janak Ramakrishnane72d5222015-02-26 17:09:18 +00002//
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
Eric Fellheimer6f8b7ce2016-01-29 00:15:44 +000016import com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe;
Klaus Aehlig777b30d2017-02-24 16:30:15 +000017import com.google.devtools.build.lib.events.ExtendedEventHandler;
Googler2dd4c182016-10-31 14:54:37 +000018import com.google.devtools.build.skyframe.QueryableGraph.Reason;
Janak Ramakrishnane72d5222015-02-26 17:09:18 +000019import java.util.Collection;
Janak Ramakrishnan36858732015-06-17 16:45:47 +000020import java.util.Map;
Googler5e65c982017-08-17 05:21:54 +020021import java.util.Set;
Janak Ramakrishnane72d5222015-02-26 17:09:18 +000022import javax.annotation.Nullable;
23
24/**
25 * Read-only graph that exposes the dependents, dependencies (reverse dependents), and value and
26 * exception (if any) of a given node.
Janak Ramakrishnan3c0adb22016-08-15 21:54:55 +000027 *
28 * <p>Certain graph implementations can throw {@link InterruptedException} when trying to retrieve
29 * node entries. Such exceptions should not be caught locally -- they should be allowed to propagate
30 * up.
Janak Ramakrishnane72d5222015-02-26 17:09:18 +000031 */
Eric Fellheimer6f8b7ce2016-01-29 00:15:44 +000032@ThreadSafe
Janak Ramakrishnane72d5222015-02-26 17:09:18 +000033public interface WalkableGraph {
Janak Ramakrishnane72d5222015-02-26 17:09:18 +000034 /**
35 * Returns the value of the given key, or {@code null} if it has no value due to an error during
Janak Ramakrishnan112840b2016-12-29 21:49:56 +000036 * its computation or it is not done in the graph.
37 *
38 * <p>A node that is done in the graph must have either a non-null getValue, a non-null {@link
39 * #getException}, or a true {@link #isCycle}.
40 *
41 * <p>These three methods should all be reading the same {@link
42 * NodeEntry#getValueMaybeWithMetadata} value internally, so once that value is indirectly
43 * retrieved via one of these methods, the others can read it for free. This is relevant for graph
44 * implementations that may throw an {@link InterruptedException} on retrieving entries and value.
Janak Ramakrishnane72d5222015-02-26 17:09:18 +000045 */
46 @Nullable
Janak Ramakrishnan3c0adb22016-08-15 21:54:55 +000047 SkyValue getValue(SkyKey key) throws InterruptedException;
Janak Ramakrishnane72d5222015-02-26 17:09:18 +000048
49 /**
Miguel Alcon Pinto45820872015-09-11 19:57:47 +000050 * Returns a map giving the values of the given keys for done keys that were successfully
Janak Ramakrishnan3c0adb22016-08-15 21:54:55 +000051 * computed. Or in other words, it filters out non-existent nodes, pending nodes and nodes that
52 * produced an exception.
Janak Ramakrishnan36858732015-06-17 16:45:47 +000053 */
Janak Ramakrishnan3c0adb22016-08-15 21:54:55 +000054 Map<SkyKey, SkyValue> getSuccessfulValues(Iterable<SkyKey> keys) throws InterruptedException;
Janak Ramakrishnanf6f0fcc2015-06-19 20:24:52 +000055
56 /**
57 * Returns a map giving exceptions associated to the given keys for done keys. Keys not present in
58 * the graph or whose nodes are not done will be present in the returned map, with null value. In
59 * other words, if {@code key} is in {@param keys}, then the returned map will contain an entry
60 * for {@code key} if and only if the node for {@code key} did <i>not</i> evaluate successfully
61 * without error.
62 */
Janak Ramakrishnan3c0adb22016-08-15 21:54:55 +000063 Map<SkyKey, Exception> getMissingAndExceptions(Iterable<SkyKey> keys) throws InterruptedException;
Janak Ramakrishnan36858732015-06-17 16:45:47 +000064
65 /**
Janak Ramakrishnane72d5222015-02-26 17:09:18 +000066 * Returns the exception thrown when computing the node with the given key, if any. If the node
Janak Ramakrishnan112840b2016-12-29 21:49:56 +000067 * was computed successfully, depends on a cycle without any other error, or is not done in the
68 * graph, returns null.
Janak Ramakrishnane72d5222015-02-26 17:09:18 +000069 */
Janak Ramakrishnan3c0adb22016-08-15 21:54:55 +000070 @Nullable
71 Exception getException(SkyKey key) throws InterruptedException;
Janak Ramakrishnane72d5222015-02-26 17:09:18 +000072
73 /**
Janak Ramakrishnan112840b2016-12-29 21:49:56 +000074 * Returns true if the node with the given {@code key} depends on a cycle. Returns false if the
75 * node does not depend on a cycle, or is not done in the graph.
76 */
77 boolean isCycle(SkyKey key) throws InterruptedException;
78
79 /**
Janak Ramakrishnancda5b662015-06-18 23:46:36 +000080 * Returns a map giving the direct dependencies of the nodes with the given keys. A node for each
Janak Ramakrishnan112840b2016-12-29 21:49:56 +000081 * given key must be done in the graph if it exists.
Janak Ramakrishnane72d5222015-02-26 17:09:18 +000082 */
Janak Ramakrishnan3c0adb22016-08-15 21:54:55 +000083 Map<SkyKey, Iterable<SkyKey>> getDirectDeps(Iterable<SkyKey> keys) throws InterruptedException;
Janak Ramakrishnan36858732015-06-17 16:45:47 +000084
Janak Ramakrishnan36858732015-06-17 16:45:47 +000085 /**
Janak Ramakrishnancda5b662015-06-18 23:46:36 +000086 * Returns a map giving the reverse dependencies of the nodes with the given keys. A node for each
Janak Ramakrishnan112840b2016-12-29 21:49:56 +000087 * given key must be done in the graph if it exists.
Janak Ramakrishnan36858732015-06-17 16:45:47 +000088 */
Janak Ramakrishnan3c0adb22016-08-15 21:54:55 +000089 Map<SkyKey, Iterable<SkyKey>> getReverseDeps(Iterable<SkyKey> keys) throws InterruptedException;
Janak Ramakrishnan36858732015-06-17 16:45:47 +000090
Googler2dd4c182016-10-31 14:54:37 +000091 /**
92 * Examines all the given keys. Returns an iterable of keys whose corresponding nodes are
93 * currently available to be fetched.
94 *
95 * <p>Note: An unavailable node does not mean it is not in the graph. It only means it's not ready
96 * to be fetched immediately.
97 */
98 Iterable<SkyKey> getCurrentlyAvailableNodes(Iterable<SkyKey> keys, Reason reason);
99
Janak Ramakrishnane72d5222015-02-26 17:09:18 +0000100 /** Provides a WalkableGraph on demand after preparing it. */
101 interface WalkableGraphFactory {
Janak Ramakrishnan18d0a5d2016-11-16 19:35:48 +0000102 EvaluationResult<SkyValue> prepareAndGet(
Googler5e65c982017-08-17 05:21:54 +0200103 Set<SkyKey> roots, int numThreads, ExtendedEventHandler eventHandler)
Klaus Aehlig777b30d2017-02-24 16:30:15 +0000104 throws InterruptedException;
Janak Ramakrishnan958ef822016-01-07 16:21:39 +0000105
106 /** Returns the {@link SkyKey} that defines this universe. */
107 SkyKey getUniverseKey(Collection<String> roots, String offset);
Janak Ramakrishnane72d5222015-02-26 17:09:18 +0000108 }
109}