Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2014 The Bazel Authors. All rights reserved. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [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 | |
shahan | 21e2600 | 2018-10-01 08:25:02 -0700 | [diff] [blame] | 16 | import com.google.common.base.Predicates; |
| 17 | import com.google.common.collect.Iterables; |
Nathan Harmata | e955287 | 2015-04-01 21:53:06 +0000 | [diff] [blame] | 18 | import com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe; |
shahan | 5de60f1 | 2018-12-07 17:06:38 -0800 | [diff] [blame] | 19 | import com.google.devtools.build.lib.util.GroupedList; |
shreyax | 26e7280 | 2018-03-26 09:04:48 -0700 | [diff] [blame] | 20 | import com.google.errorprone.annotations.CanIgnoreReturnValue; |
Nathan Harmata | 6fc9fa0 | 2015-03-27 17:48:57 +0000 | [diff] [blame] | 21 | import java.util.Map; |
shahan | 21e2600 | 2018-10-01 08:25:02 -0700 | [diff] [blame] | 22 | import java.util.Set; |
Nathan Harmata | 6fc9fa0 | 2015-03-27 17:48:57 +0000 | [diff] [blame] | 23 | import javax.annotation.Nullable; |
| 24 | |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 25 | /** |
| 26 | * A graph that exposes its entries and structure, for use by classes that must traverse it. |
| 27 | * |
| 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. |
| 31 | */ |
Nathan Harmata | e955287 | 2015-04-01 21:53:06 +0000 | [diff] [blame] | 32 | @ThreadSafe |
Janak Ramakrishnan | cc7712f | 2016-07-08 17:38:27 +0000 | [diff] [blame] | 33 | public interface QueryableGraph { |
Nathan Harmata | 3b47b1f | 2016-07-26 18:41:41 +0000 | [diff] [blame] | 34 | /** |
| 35 | * Returns the node with the given {@code key}, or {@code null} if the node does not exist. |
| 36 | * |
| 37 | * @param requestor if non-{@code null}, the node on behalf of which {@code key} is being |
| 38 | * requested. |
| 39 | * @param reason the reason the node is being requested. |
| 40 | */ |
Nathan Harmata | 6fc9fa0 | 2015-03-27 17:48:57 +0000 | [diff] [blame] | 41 | @Nullable |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 42 | NodeEntry get(@Nullable SkyKey requestor, Reason reason, SkyKey key) throws InterruptedException; |
Nathan Harmata | 6fc9fa0 | 2015-03-27 17:48:57 +0000 | [diff] [blame] | 43 | |
| 44 | /** |
Janak Ramakrishnan | 2553c84 | 2016-07-08 18:43:37 +0000 | [diff] [blame] | 45 | * Fetches all the given nodes. Returns a map {@code m} such that, for all {@code k} in {@code |
| 46 | * keys}, {@code m.get(k).equals(e)} iff {@code get(k) == e} and {@code e != null}, and {@code |
Nathan Harmata | c55fe15 | 2016-08-02 18:13:28 +0000 | [diff] [blame] | 47 | * !m.containsKey(k)} iff {@code get(k) == null}. |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 48 | * |
Nathan Harmata | 3b47b1f | 2016-07-26 18:41:41 +0000 | [diff] [blame] | 49 | * @param requestor if non-{@code null}, the node on behalf of which the given {@code keys} are |
| 50 | * being requested. |
| 51 | * @param reason the reason the nodes are being requested. |
Nathan Harmata | 6fc9fa0 | 2015-03-27 17:48:57 +0000 | [diff] [blame] | 52 | */ |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 53 | Map<SkyKey, ? extends NodeEntry> getBatch( |
ulfjack | be83f13 | 2017-07-18 18:12:09 +0200 | [diff] [blame] | 54 | @Nullable SkyKey requestor, Reason reason, Iterable<? extends SkyKey> keys) |
| 55 | throws InterruptedException; |
Nathan Harmata | 3b47b1f | 2016-07-26 18:41:41 +0000 | [diff] [blame] | 56 | |
| 57 | /** |
shreyax | 26e7280 | 2018-03-26 09:04:48 -0700 | [diff] [blame] | 58 | * A version of {@link #getBatch} that returns an {@link InterruptibleSupplier} to possibly |
| 59 | * retrieve the results later. |
shreyax | a6679ae | 2018-03-02 15:59:46 -0800 | [diff] [blame] | 60 | */ |
shreyax | 26e7280 | 2018-03-26 09:04:48 -0700 | [diff] [blame] | 61 | @CanIgnoreReturnValue |
| 62 | default InterruptibleSupplier<Map<SkyKey, ? extends NodeEntry>> getBatchAsync( |
shreyax | a6679ae | 2018-03-02 15:59:46 -0800 | [diff] [blame] | 63 | @Nullable SkyKey requestor, Reason reason, Iterable<? extends SkyKey> keys) { |
shreyax | 26e7280 | 2018-03-26 09:04:48 -0700 | [diff] [blame] | 64 | return InterruptibleSupplier.Memoize.of(() -> getBatch(requestor, reason, keys)); |
shreyax | a6679ae | 2018-03-02 15:59:46 -0800 | [diff] [blame] | 65 | } |
| 66 | |
shahan | 21e2600 | 2018-10-01 08:25:02 -0700 | [diff] [blame] | 67 | /** |
| 68 | * Optimistically prefetches dependencies. |
| 69 | * |
shahan | 5de60f1 | 2018-12-07 17:06:38 -0800 | [diff] [blame] | 70 | * @see PrefetchDepsRequest |
shahan | 21e2600 | 2018-10-01 08:25:02 -0700 | [diff] [blame] | 71 | */ |
shahan | 5de60f1 | 2018-12-07 17:06:38 -0800 | [diff] [blame] | 72 | default void prefetchDeps(PrefetchDepsRequest request) throws InterruptedException { |
| 73 | if (request.oldDeps.isEmpty()) { |
| 74 | return; |
| 75 | } |
| 76 | request.excludedKeys = request.depKeys.toSet(); |
shahan | 21e2600 | 2018-10-01 08:25:02 -0700 | [diff] [blame] | 77 | getBatchAsync( |
shahan | 5de60f1 | 2018-12-07 17:06:38 -0800 | [diff] [blame] | 78 | request.requestor, |
shahan | 21e2600 | 2018-10-01 08:25:02 -0700 | [diff] [blame] | 79 | Reason.PREFETCH, |
shahan | 5de60f1 | 2018-12-07 17:06:38 -0800 | [diff] [blame] | 80 | Iterables.filter(request.oldDeps, Predicates.not(Predicates.in(request.excludedKeys)))); |
shahan | 5233993 | 2018-09-16 10:33:59 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Googler | 039630a | 2018-12-26 09:41:12 -0800 | [diff] [blame] | 83 | /** Checks whether this graph stores reverse dependencies. */ |
| 84 | default boolean storesReverseDeps() { |
| 85 | return true; |
| 86 | } |
| 87 | |
shreyax | a6679ae | 2018-03-02 15:59:46 -0800 | [diff] [blame] | 88 | /** |
Nathan Harmata | 3b47b1f | 2016-07-26 18:41:41 +0000 | [diff] [blame] | 89 | * The reason that a node is being looked up in the Skyframe graph. |
| 90 | * |
| 91 | * <p>Alternate graph implementations may wish to make use of this information. |
| 92 | */ |
| 93 | enum Reason { |
| 94 | /** |
Nathan Harmata | c55fe15 | 2016-08-02 18:13:28 +0000 | [diff] [blame] | 95 | * The node is being fetched in order to see if it needs to be evaluated or because it was just |
| 96 | * evaluated, but *not* because it was just requested during evaluation of a SkyFunction |
| 97 | * (see {@link #DEP_REQUESTED}). |
| 98 | */ |
| 99 | PRE_OR_POST_EVALUATION, |
| 100 | |
| 101 | /** |
Nathan Harmata | 3b47b1f | 2016-07-26 18:41:41 +0000 | [diff] [blame] | 102 | * The node is being looked up as part of the prefetch step before evaluation of a SkyFunction. |
| 103 | */ |
| 104 | PREFETCH, |
| 105 | |
| 106 | /** |
Nathan Harmata | c55fe15 | 2016-08-02 18:13:28 +0000 | [diff] [blame] | 107 | * The node is being fetched because it is about to be evaluated, but *not* because it was just |
| 108 | * requested during evaluation of a SkyFunction (see {@link #DEP_REQUESTED}). |
Nathan Harmata | 3b47b1f | 2016-07-26 18:41:41 +0000 | [diff] [blame] | 109 | */ |
| 110 | EVALUATION, |
| 111 | |
| 112 | /** The node is being looked up because it was requested during evaluation of a SkyFunction. */ |
| 113 | DEP_REQUESTED, |
| 114 | |
| 115 | /** The node is being looked up during the invalidation phase of Skyframe evaluation. */ |
| 116 | INVALIDATION, |
| 117 | |
| 118 | /** The node is being looked up during the cycle checking phase of Skyframe evaluation. */ |
| 119 | CYCLE_CHECKING, |
| 120 | |
| 121 | /** The node is being looked up so that an rdep can be added to it. */ |
| 122 | RDEP_ADDITION, |
| 123 | |
| 124 | /** The node is being looked up so that an rdep can be removed from it. */ |
| 125 | RDEP_REMOVAL, |
| 126 | |
Shreya Bhattarai | 7fd3f2c5 | 2017-02-09 01:59:28 +0000 | [diff] [blame] | 127 | /** The node is being looked up for any graph clean-up effort that may be necessary. */ |
| 128 | CLEAN_UP, |
| 129 | |
Nathan Harmata | 3b47b1f | 2016-07-26 18:41:41 +0000 | [diff] [blame] | 130 | /** The node is being looked up so it can be enqueued for evaluation or change pruning. */ |
| 131 | ENQUEUING_CHILD, |
| 132 | |
| 133 | /** |
| 134 | * The node is being looked up so that it can be signaled that a dependency is now complete. |
| 135 | */ |
| 136 | SIGNAL_DEP, |
| 137 | |
| 138 | /** |
| 139 | * The node is being looking up as part of the error bubbling phase of fail-fast Skyframe |
| 140 | * evaluation. |
| 141 | */ |
| 142 | ERROR_BUBBLING, |
| 143 | |
Nathan Harmata | c55fe15 | 2016-08-02 18:13:28 +0000 | [diff] [blame] | 144 | /** The node is being looked up merely for an existence check. */ |
| 145 | EXISTENCE_CHECKING, |
| 146 | |
shreyax | 26e7280 | 2018-03-26 09:04:48 -0700 | [diff] [blame] | 147 | /** The node is being looked up merely to see if it is done or not. */ |
| 148 | DONE_CHECKING, |
| 149 | |
Nathan Harmata | c55fe15 | 2016-08-02 18:13:28 +0000 | [diff] [blame] | 150 | /** |
| 151 | * The node is being looked up to service {@link WalkableGraph#getValue}, |
| 152 | * {@link WalkableGraph#getException}, {@link WalkableGraph#getMissingAndExceptions}, or |
| 153 | * {@link WalkableGraph#getSuccessfulValues}. |
| 154 | */ |
| 155 | WALKABLE_GRAPH_VALUE, |
| 156 | |
| 157 | /** The node is being looked up to service {@link WalkableGraph#getDirectDeps}. */ |
| 158 | WALKABLE_GRAPH_DEPS, |
| 159 | |
| 160 | /** The node is being looked up to service {@link WalkableGraph#getReverseDeps}. */ |
| 161 | WALKABLE_GRAPH_RDEPS, |
| 162 | |
Nathan Harmata | 3b47b1f | 2016-07-26 18:41:41 +0000 | [diff] [blame] | 163 | /** Some other reason than one of the above. */ |
| 164 | OTHER, |
| 165 | } |
shahan | 5de60f1 | 2018-12-07 17:06:38 -0800 | [diff] [blame] | 166 | |
| 167 | /** Parameters for {@link QueryableGraph#prefetchDeps}. */ |
| 168 | static class PrefetchDepsRequest { |
| 169 | public final SkyKey requestor; |
| 170 | |
| 171 | /** |
| 172 | * Old dependencies to prefetch. |
| 173 | * |
| 174 | * <p>The implementation might ignore this if it has another way to determine the dependencies. |
| 175 | */ |
| 176 | public final Set<SkyKey> oldDeps; |
| 177 | |
| 178 | /** |
| 179 | * Direct deps that will be subsequently fetched and therefore should be excluded from |
| 180 | * prefetching. |
| 181 | */ |
| 182 | public final GroupedList<SkyKey> depKeys; |
| 183 | |
| 184 | /** |
| 185 | * Output parameter: {@code depKeys} as a set. |
| 186 | * |
| 187 | * <p>The implementation might set this, in which case, the caller could reuse it. |
| 188 | */ |
| 189 | @Nullable public Set<SkyKey> excludedKeys = null; |
| 190 | |
| 191 | public PrefetchDepsRequest(SkyKey requestor, Set<SkyKey> oldDeps, GroupedList<SkyKey> depKeys) { |
| 192 | this.requestor = requestor; |
| 193 | this.oldDeps = oldDeps; |
| 194 | this.depKeys = depKeys; |
| 195 | } |
| 196 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 197 | } |