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 | |
Mark Schaller | e3c88de | 2015-08-05 22:02:59 +0000 | [diff] [blame] | 16 | import com.google.common.base.Function; |
| 17 | import com.google.devtools.build.lib.concurrent.AbstractQueueVisitor; |
Nathan Harmata | 3724d92 | 2016-05-26 19:56:24 +0000 | [diff] [blame] | 18 | import com.google.devtools.build.lib.concurrent.ErrorHandler; |
Mark Schaller | eff2b45 | 2015-10-13 20:06:19 +0000 | [diff] [blame] | 19 | import com.google.devtools.build.lib.concurrent.ExecutorParams; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 20 | import com.google.devtools.build.skyframe.InvalidatingNodeVisitor.DeletingNodeVisitor; |
| 21 | import com.google.devtools.build.skyframe.InvalidatingNodeVisitor.DirtyingNodeVisitor; |
| 22 | import com.google.devtools.build.skyframe.InvalidatingNodeVisitor.InvalidationState; |
Mark Schaller | eff2b45 | 2015-10-13 20:06:19 +0000 | [diff] [blame] | 23 | import java.util.concurrent.ExecutorService; |
Mark Schaller | 454bc6d | 2015-11-03 19:12:48 +0000 | [diff] [blame] | 24 | import java.util.concurrent.ForkJoinPool; |
Mark Schaller | e3c88de | 2015-08-05 22:02:59 +0000 | [diff] [blame] | 25 | import javax.annotation.Nullable; |
| 26 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 27 | /** |
| 28 | * Utility class for performing eager invalidation on Skyframe graphs. |
| 29 | * |
| 30 | * <p>This is intended only for use in alternative {@code MemoizingEvaluator} implementations. |
| 31 | */ |
| 32 | public final class EagerInvalidator { |
| 33 | |
| 34 | private EagerInvalidator() {} |
| 35 | |
| 36 | /** |
| 37 | * Deletes given values. The {@code traverseGraph} parameter controls whether this method deletes |
| 38 | * (transitive) dependents of these nodes and relevant graph edges, or just the nodes themselves. |
| 39 | * Deleting just the nodes is inconsistent unless the graph will not be used for incremental |
| 40 | * builds in the future, but unfortunately there is a case where we delete nodes intra-build. As |
| 41 | * long as the full upward transitive closure of the nodes is specified for deletion, the graph |
| 42 | * remains consistent. |
| 43 | */ |
Janak Ramakrishnan | cc7712f | 2016-07-08 17:38:27 +0000 | [diff] [blame] | 44 | public static void delete( |
| 45 | InMemoryGraph graph, |
| 46 | Iterable<SkyKey> diff, |
Chloe Calvarin | 860b8d2 | 2016-10-05 22:52:55 +0000 | [diff] [blame] | 47 | DirtyTrackingProgressReceiver progressReceiver, |
Janak Ramakrishnan | cc7712f | 2016-07-08 17:38:27 +0000 | [diff] [blame] | 48 | InvalidationState state, |
Chloe Calvarin | 860b8d2 | 2016-10-05 22:52:55 +0000 | [diff] [blame] | 49 | boolean traverseGraph) |
Janak Ramakrishnan | cc7712f | 2016-07-08 17:38:27 +0000 | [diff] [blame] | 50 | throws InterruptedException { |
Mark Schaller | 226ce681 | 2015-09-01 22:44:58 +0000 | [diff] [blame] | 51 | DeletingNodeVisitor visitor = |
| 52 | createDeletingVisitorIfNeeded( |
Chloe Calvarin | 860b8d2 | 2016-10-05 22:52:55 +0000 | [diff] [blame] | 53 | graph, diff, progressReceiver, state, traverseGraph); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 54 | if (visitor != null) { |
| 55 | visitor.run(); |
| 56 | } |
| 57 | } |
| 58 | |
Mark Schaller | e3c88de | 2015-08-05 22:02:59 +0000 | [diff] [blame] | 59 | @Nullable |
Mark Schaller | 226ce681 | 2015-09-01 22:44:58 +0000 | [diff] [blame] | 60 | static DeletingNodeVisitor createDeletingVisitorIfNeeded( |
Janak Ramakrishnan | cc7712f | 2016-07-08 17:38:27 +0000 | [diff] [blame] | 61 | InMemoryGraph graph, |
Mark Schaller | 226ce681 | 2015-09-01 22:44:58 +0000 | [diff] [blame] | 62 | Iterable<SkyKey> diff, |
Chloe Calvarin | 860b8d2 | 2016-10-05 22:52:55 +0000 | [diff] [blame] | 63 | DirtyTrackingProgressReceiver progressReceiver, |
Mark Schaller | 226ce681 | 2015-09-01 22:44:58 +0000 | [diff] [blame] | 64 | InvalidationState state, |
Chloe Calvarin | 860b8d2 | 2016-10-05 22:52:55 +0000 | [diff] [blame] | 65 | boolean traverseGraph) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 66 | state.update(diff); |
Mark Schaller | e3c88de | 2015-08-05 22:02:59 +0000 | [diff] [blame] | 67 | return state.isEmpty() ? null |
Chloe Calvarin | 860b8d2 | 2016-10-05 22:52:55 +0000 | [diff] [blame] | 68 | : new DeletingNodeVisitor(graph, progressReceiver, state, traverseGraph); |
Mark Schaller | e3c88de | 2015-08-05 22:02:59 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | @Nullable |
Mark Schaller | 226ce681 | 2015-09-01 22:44:58 +0000 | [diff] [blame] | 72 | static DirtyingNodeVisitor createInvalidatingVisitorIfNeeded( |
Nathan Harmata | 33faf91 | 2016-08-10 19:15:15 +0000 | [diff] [blame] | 73 | QueryableGraph graph, |
Mark Schaller | 226ce681 | 2015-09-01 22:44:58 +0000 | [diff] [blame] | 74 | Iterable<SkyKey> diff, |
Chloe Calvarin | 860b8d2 | 2016-10-05 22:52:55 +0000 | [diff] [blame] | 75 | DirtyTrackingProgressReceiver progressReceiver, |
Mark Schaller | 226ce681 | 2015-09-01 22:44:58 +0000 | [diff] [blame] | 76 | InvalidationState state, |
Mark Schaller | eff2b45 | 2015-10-13 20:06:19 +0000 | [diff] [blame] | 77 | Function<ExecutorParams, ? extends ExecutorService> executorFactory) { |
Mark Schaller | e3c88de | 2015-08-05 22:02:59 +0000 | [diff] [blame] | 78 | state.update(diff); |
| 79 | return state.isEmpty() ? null |
Chloe Calvarin | 860b8d2 | 2016-10-05 22:52:55 +0000 | [diff] [blame] | 80 | : new DirtyingNodeVisitor(graph, progressReceiver, state, executorFactory); |
Mark Schaller | e3c88de | 2015-08-05 22:02:59 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Mark Schaller | 454bc6d | 2015-11-03 19:12:48 +0000 | [diff] [blame] | 83 | @Nullable |
| 84 | private static DirtyingNodeVisitor createInvalidatingVisitorIfNeeded( |
Nathan Harmata | 33faf91 | 2016-08-10 19:15:15 +0000 | [diff] [blame] | 85 | QueryableGraph graph, |
Mark Schaller | 454bc6d | 2015-11-03 19:12:48 +0000 | [diff] [blame] | 86 | Iterable<SkyKey> diff, |
Chloe Calvarin | 860b8d2 | 2016-10-05 22:52:55 +0000 | [diff] [blame] | 87 | DirtyTrackingProgressReceiver progressReceiver, |
Mark Schaller | 454bc6d | 2015-11-03 19:12:48 +0000 | [diff] [blame] | 88 | InvalidationState state, |
Mark Schaller | c6ed613 | 2015-11-24 15:45:40 +0000 | [diff] [blame] | 89 | ForkJoinPool forkJoinPool, |
Nathan Harmata | 3724d92 | 2016-05-26 19:56:24 +0000 | [diff] [blame] | 90 | boolean supportInterruptions, |
| 91 | ErrorHandler errorHandler) { |
Mark Schaller | 454bc6d | 2015-11-03 19:12:48 +0000 | [diff] [blame] | 92 | state.update(diff); |
| 93 | return state.isEmpty() |
| 94 | ? null |
| 95 | : new DirtyingNodeVisitor( |
Mark Schaller | c6ed613 | 2015-11-24 15:45:40 +0000 | [diff] [blame] | 96 | graph, |
Googler | c4e2255 | 2016-09-27 15:44:40 +0000 | [diff] [blame] | 97 | progressReceiver, |
Mark Schaller | c6ed613 | 2015-11-24 15:45:40 +0000 | [diff] [blame] | 98 | state, |
Mark Schaller | c6ed613 | 2015-11-24 15:45:40 +0000 | [diff] [blame] | 99 | forkJoinPool, |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 100 | supportInterruptions); |
Mark Schaller | 454bc6d | 2015-11-03 19:12:48 +0000 | [diff] [blame] | 101 | } |
Mark Schaller | e3c88de | 2015-08-05 22:02:59 +0000 | [diff] [blame] | 102 | /** |
Mark Schaller | 454bc6d | 2015-11-03 19:12:48 +0000 | [diff] [blame] | 103 | * Invalidates given values and their upward transitive closure in the graph if necessary, using |
| 104 | * an executor constructed with the provided factory. |
Mark Schaller | e3c88de | 2015-08-05 22:02:59 +0000 | [diff] [blame] | 105 | */ |
Mark Schaller | 226ce681 | 2015-09-01 22:44:58 +0000 | [diff] [blame] | 106 | public static void invalidate( |
Nathan Harmata | 33faf91 | 2016-08-10 19:15:15 +0000 | [diff] [blame] | 107 | QueryableGraph graph, |
Mark Schaller | 226ce681 | 2015-09-01 22:44:58 +0000 | [diff] [blame] | 108 | Iterable<SkyKey> diff, |
Chloe Calvarin | 860b8d2 | 2016-10-05 22:52:55 +0000 | [diff] [blame] | 109 | DirtyTrackingProgressReceiver progressReceiver, |
Mark Schaller | 226ce681 | 2015-09-01 22:44:58 +0000 | [diff] [blame] | 110 | InvalidationState state, |
Mark Schaller | eff2b45 | 2015-10-13 20:06:19 +0000 | [diff] [blame] | 111 | Function<ExecutorParams, ? extends ExecutorService> executorFactory) |
Mark Schaller | 226ce681 | 2015-09-01 22:44:58 +0000 | [diff] [blame] | 112 | throws InterruptedException { |
Mark Schaller | 226ce681 | 2015-09-01 22:44:58 +0000 | [diff] [blame] | 113 | DirtyingNodeVisitor visitor = |
| 114 | createInvalidatingVisitorIfNeeded( |
Chloe Calvarin | 860b8d2 | 2016-10-05 22:52:55 +0000 | [diff] [blame] | 115 | graph, diff, progressReceiver, state, executorFactory); |
Mark Schaller | e3c88de | 2015-08-05 22:02:59 +0000 | [diff] [blame] | 116 | if (visitor != null) { |
| 117 | visitor.run(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 118 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | /** |
Mark Schaller | 454bc6d | 2015-11-03 19:12:48 +0000 | [diff] [blame] | 122 | * Invalidates given values and their upward transitive closure in the graph if necessary, using |
| 123 | * the provided {@link ForkJoinPool}. |
| 124 | */ |
| 125 | public static void invalidate( |
Nathan Harmata | 33faf91 | 2016-08-10 19:15:15 +0000 | [diff] [blame] | 126 | QueryableGraph graph, |
Mark Schaller | 454bc6d | 2015-11-03 19:12:48 +0000 | [diff] [blame] | 127 | Iterable<SkyKey> diff, |
Chloe Calvarin | 860b8d2 | 2016-10-05 22:52:55 +0000 | [diff] [blame] | 128 | DirtyTrackingProgressReceiver progressReceiver, |
Mark Schaller | 454bc6d | 2015-11-03 19:12:48 +0000 | [diff] [blame] | 129 | InvalidationState state, |
Mark Schaller | c6ed613 | 2015-11-24 15:45:40 +0000 | [diff] [blame] | 130 | ForkJoinPool forkJoinPool, |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 131 | boolean supportInterruptions) |
Mark Schaller | 454bc6d | 2015-11-03 19:12:48 +0000 | [diff] [blame] | 132 | throws InterruptedException { |
| 133 | DirtyingNodeVisitor visitor = |
| 134 | createInvalidatingVisitorIfNeeded( |
Mark Schaller | c6ed613 | 2015-11-24 15:45:40 +0000 | [diff] [blame] | 135 | graph, |
| 136 | diff, |
Googler | c4e2255 | 2016-09-27 15:44:40 +0000 | [diff] [blame] | 137 | progressReceiver, |
Mark Schaller | c6ed613 | 2015-11-24 15:45:40 +0000 | [diff] [blame] | 138 | state, |
Mark Schaller | c6ed613 | 2015-11-24 15:45:40 +0000 | [diff] [blame] | 139 | forkJoinPool, |
Nathan Harmata | 3724d92 | 2016-05-26 19:56:24 +0000 | [diff] [blame] | 140 | supportInterruptions, |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 141 | ErrorHandler.NullHandler.INSTANCE); |
Mark Schaller | 454bc6d | 2015-11-03 19:12:48 +0000 | [diff] [blame] | 142 | if (visitor != null) { |
| 143 | visitor.run(); |
| 144 | } |
| 145 | } |
| 146 | |
Janak Ramakrishnan | cc7712f | 2016-07-08 17:38:27 +0000 | [diff] [blame] | 147 | /** Invalidates given values and their upward transitive closure in the graph. */ |
Mark Schaller | 454bc6d | 2015-11-03 19:12:48 +0000 | [diff] [blame] | 148 | public static void invalidate( |
Nathan Harmata | 33faf91 | 2016-08-10 19:15:15 +0000 | [diff] [blame] | 149 | QueryableGraph graph, |
Mark Schaller | 454bc6d | 2015-11-03 19:12:48 +0000 | [diff] [blame] | 150 | Iterable<SkyKey> diff, |
Chloe Calvarin | 860b8d2 | 2016-10-05 22:52:55 +0000 | [diff] [blame] | 151 | DirtyTrackingProgressReceiver progressReceiver, |
| 152 | InvalidationState state) |
Mark Schaller | e3c88de | 2015-08-05 22:02:59 +0000 | [diff] [blame] | 153 | throws InterruptedException { |
Chloe Calvarin | 860b8d2 | 2016-10-05 22:52:55 +0000 | [diff] [blame] | 154 | invalidate(graph, diff, progressReceiver, state, AbstractQueueVisitor.EXECUTOR_FACTORY); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 155 | } |
Mark Schaller | e3c88de | 2015-08-05 22:02:59 +0000 | [diff] [blame] | 156 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 157 | } |