Googler | 1590dbc | 2023-06-08 15:38:51 -0700 | [diff] [blame] | 1 | // Copyright 2023 The Bazel Authors. 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. |
| 14 | |
Googler | 23e5ec1 | 2024-08-30 11:27:18 -0700 | [diff] [blame] | 15 | package com.google.devtools.build.skyframe; |
Googler | 1590dbc | 2023-06-08 15:38:51 -0700 | [diff] [blame] | 16 | |
| 17 | import com.google.common.collect.ImmutableList; |
| 18 | import com.google.common.util.concurrent.ListenableFuture; |
| 19 | import com.google.devtools.build.lib.events.Event; |
| 20 | import com.google.devtools.build.lib.events.ExtendedEventHandler; |
Googler | 23e5ec1 | 2024-08-30 11:27:18 -0700 | [diff] [blame] | 21 | import com.google.devtools.build.lib.supplier.InterruptibleSupplier; |
Googler | 1590dbc | 2023-06-08 15:38:51 -0700 | [diff] [blame] | 22 | import java.util.function.Supplier; |
| 23 | import javax.annotation.Nullable; |
| 24 | |
| 25 | /** |
Googler | d24f947 | 2024-08-29 12:31:18 -0700 | [diff] [blame] | 26 | * A {@link SkyFunction.Environment} implementation designed to be used in a different thread (the |
| 27 | * "worker thread") than the corresponding SkyFunction runs in. It relies on a delegate Environment |
| 28 | * object to do underlying work. Its {@link #getValue} and {@link #getValueOrThrow} methods do not |
| 29 | * return {@code null} when the {@link SkyValue} in question is not available. Instead, it blocks |
| 30 | * and waits for the host Skyframe thread to restart, and replaces the delegate Environment with a |
| 31 | * fresh one from the restarted SkyFunction before continuing. (Note that those methods <em>do</em> |
| 32 | * return {@code null} if the SkyValue was evaluated but found to be in error.) |
Googler | 1590dbc | 2023-06-08 15:38:51 -0700 | [diff] [blame] | 33 | * |
| 34 | * <p>Crucially, the delegate Environment object must not be used by multiple threads at the same |
| 35 | * time. In effect, this is guaranteed by only one of the worker thread and host thread being active |
| 36 | * at any given time. |
| 37 | */ |
Googler | d24f947 | 2024-08-29 12:31:18 -0700 | [diff] [blame] | 38 | class WorkerSkyFunctionEnvironment |
Googler | 1590dbc | 2023-06-08 15:38:51 -0700 | [diff] [blame] | 39 | implements SkyFunction.Environment, ExtendedEventHandler, SkyframeLookupResult { |
Googler | 1590dbc | 2023-06-08 15:38:51 -0700 | [diff] [blame] | 40 | private SkyFunction.Environment delegate; |
Googler | 23e5ec1 | 2024-08-30 11:27:18 -0700 | [diff] [blame] | 41 | private final InterruptibleSupplier<SkyFunction.Environment> newDelegateSupplier; |
Googler | 1590dbc | 2023-06-08 15:38:51 -0700 | [diff] [blame] | 42 | |
Googler | 23e5ec1 | 2024-08-30 11:27:18 -0700 | [diff] [blame] | 43 | WorkerSkyFunctionEnvironment( |
| 44 | SkyFunction.Environment initialDelegate, |
| 45 | InterruptibleSupplier<SkyFunction.Environment> newDelegateSupplier) { |
| 46 | this.delegate = initialDelegate; |
| 47 | this.newDelegateSupplier = newDelegateSupplier; |
Googler | 1590dbc | 2023-06-08 15:38:51 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | @Override |
| 51 | public boolean valuesMissing() { |
| 52 | return delegate.valuesMissing(); |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public SkyframeLookupResult getValuesAndExceptions(Iterable<? extends SkyKey> depKeys) |
| 57 | throws InterruptedException { |
| 58 | delegate.getValuesAndExceptions(depKeys); |
| 59 | if (!delegate.valuesMissing()) { |
| 60 | // Do NOT just return the return value of `delegate.getValuesAndExceptions` here! That would |
Googler | 23e5ec1 | 2024-08-30 11:27:18 -0700 | [diff] [blame] | 61 | // cause anyone holding onto the returned result object to potentially use a stale version |
Googler | 1590dbc | 2023-06-08 15:38:51 -0700 | [diff] [blame] | 62 | // of it after a skyfunction restart. |
| 63 | return this; |
| 64 | } |
| 65 | // We null out `delegate` before blocking for the fresh env so that the old one becomes |
| 66 | // eligible for GC. |
| 67 | delegate = null; |
Googler | 23e5ec1 | 2024-08-30 11:27:18 -0700 | [diff] [blame] | 68 | delegate = newDelegateSupplier.get(); |
Googler | 1590dbc | 2023-06-08 15:38:51 -0700 | [diff] [blame] | 69 | delegate.getValuesAndExceptions(depKeys); |
| 70 | return this; |
| 71 | } |
| 72 | |
| 73 | @Nullable |
| 74 | @Override |
| 75 | public <E1 extends Exception, E2 extends Exception, E3 extends Exception> SkyValue getOrThrow( |
| 76 | SkyKey skyKey, Class<E1> e1, Class<E2> e2, Class<E3> e3) throws E1, E2, E3 { |
| 77 | return delegate.getLookupHandleForPreviouslyRequestedDeps().getOrThrow(skyKey, e1, e2, e3); |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public boolean queryDep(SkyKey key, QueryDepCallback resultCallback) { |
| 82 | return delegate.getLookupHandleForPreviouslyRequestedDeps().queryDep(key, resultCallback); |
| 83 | } |
| 84 | |
| 85 | @Nullable |
| 86 | @Override |
| 87 | public SkyValue getValue(SkyKey depKey) throws InterruptedException { |
| 88 | return getValuesAndExceptions(ImmutableList.of(depKey)).get(depKey); |
| 89 | } |
| 90 | |
| 91 | @Nullable |
| 92 | @Override |
| 93 | public <E1 extends Exception> SkyValue getValueOrThrow(SkyKey depKey, Class<E1> e1) |
| 94 | throws E1, InterruptedException { |
| 95 | return getValuesAndExceptions(ImmutableList.of(depKey)).getOrThrow(depKey, e1); |
| 96 | } |
| 97 | |
| 98 | @Nullable |
| 99 | @Override |
| 100 | public <E1 extends Exception, E2 extends Exception> SkyValue getValueOrThrow( |
| 101 | SkyKey depKey, Class<E1> e1, Class<E2> e2) throws E1, E2, InterruptedException { |
| 102 | return getValuesAndExceptions(ImmutableList.of(depKey)).getOrThrow(depKey, e1, e2); |
| 103 | } |
| 104 | |
| 105 | @Nullable |
| 106 | @Override |
| 107 | public <E1 extends Exception, E2 extends Exception, E3 extends Exception> |
| 108 | SkyValue getValueOrThrow(SkyKey depKey, Class<E1> e1, Class<E2> e2, Class<E3> e3) |
| 109 | throws E1, E2, E3, InterruptedException { |
| 110 | return getValuesAndExceptions(ImmutableList.of(depKey)).getOrThrow(depKey, e1, e2, e3); |
| 111 | } |
| 112 | |
| 113 | @Nullable |
| 114 | @Override |
| 115 | public <E1 extends Exception, E2 extends Exception, E3 extends Exception, E4 extends Exception> |
| 116 | SkyValue getValueOrThrow( |
| 117 | SkyKey depKey, Class<E1> e1, Class<E2> e2, Class<E3> e3, Class<E4> e4) |
| 118 | throws E1, E2, E3, E4, InterruptedException { |
| 119 | SkyValue value = delegate.getValueOrThrow(depKey, e1, e2, e3, e4); |
| 120 | if (value != null) { |
| 121 | return value; |
| 122 | } |
| 123 | // We null out `delegate` before blocking for the fresh env so that the old one becomes |
| 124 | // eligible for GC. |
| 125 | delegate = null; |
Googler | 23e5ec1 | 2024-08-30 11:27:18 -0700 | [diff] [blame] | 126 | delegate = newDelegateSupplier.get(); |
Googler | 1590dbc | 2023-06-08 15:38:51 -0700 | [diff] [blame] | 127 | return delegate.getValueOrThrow(depKey, e1, e2, e3, e4); |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public ExtendedEventHandler getListener() { |
| 132 | // Do NOT just return `delegate.getListener()` here! That would cause anyone holding onto the |
| 133 | // returned listener to potentially post events to a stale listener. |
| 134 | return this; |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public void post(Postable obj) { |
| 139 | delegate.getListener().post(obj); |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public void handle(Event event) { |
| 144 | delegate.getListener().handle(event); |
| 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public void registerDependencies(Iterable<SkyKey> keys) { |
| 149 | delegate.registerDependencies(keys); |
| 150 | } |
| 151 | |
| 152 | @Override |
Googler | 589e2d3 | 2024-07-19 11:58:48 -0700 | [diff] [blame] | 153 | public boolean inErrorBubbling() { |
| 154 | return delegate.inErrorBubbling(); |
Googler | 1590dbc | 2023-06-08 15:38:51 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | @Override |
| 158 | public void dependOnFuture(ListenableFuture<?> future) { |
| 159 | delegate.dependOnFuture(future); |
| 160 | } |
| 161 | |
| 162 | @Override |
Googler | 1590dbc | 2023-06-08 15:38:51 -0700 | [diff] [blame] | 163 | public SkyframeLookupResult getLookupHandleForPreviouslyRequestedDeps() { |
| 164 | return delegate.getLookupHandleForPreviouslyRequestedDeps(); |
| 165 | } |
| 166 | |
| 167 | @Override |
| 168 | public <T extends SkyKeyComputeState> T getState(Supplier<T> stateSupplier) { |
| 169 | return delegate.getState(stateSupplier); |
| 170 | } |
| 171 | |
| 172 | @Nullable |
| 173 | @Override |
| 174 | public Version getMaxTransitiveSourceVersionSoFar() { |
| 175 | return delegate.getMaxTransitiveSourceVersionSoFar(); |
| 176 | } |
| 177 | } |