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