blob: 1ce7617f590d7c2a35020d3e27d8ade5df0859ce [file] [log] [blame]
Googler1590dbc2023-06-08 15:38:51 -07001// 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
Googler23e5ec12024-08-30 11:27:18 -070015package com.google.devtools.build.skyframe;
Googler1590dbc2023-06-08 15:38:51 -070016
17import com.google.common.collect.ImmutableList;
18import com.google.common.util.concurrent.ListenableFuture;
19import com.google.devtools.build.lib.events.Event;
20import com.google.devtools.build.lib.events.ExtendedEventHandler;
Googler23e5ec12024-08-30 11:27:18 -070021import com.google.devtools.build.lib.supplier.InterruptibleSupplier;
Googler1590dbc2023-06-08 15:38:51 -070022import java.util.function.Supplier;
23import javax.annotation.Nullable;
24
25/**
Googlerd24f9472024-08-29 12:31:18 -070026 * 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.)
Googler1590dbc2023-06-08 15:38:51 -070033 *
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 */
Googlerd24f9472024-08-29 12:31:18 -070038class WorkerSkyFunctionEnvironment
Googler1590dbc2023-06-08 15:38:51 -070039 implements SkyFunction.Environment, ExtendedEventHandler, SkyframeLookupResult {
Googler1590dbc2023-06-08 15:38:51 -070040 private SkyFunction.Environment delegate;
Googler23e5ec12024-08-30 11:27:18 -070041 private final InterruptibleSupplier<SkyFunction.Environment> newDelegateSupplier;
Googler1590dbc2023-06-08 15:38:51 -070042
Googler23e5ec12024-08-30 11:27:18 -070043 WorkerSkyFunctionEnvironment(
44 SkyFunction.Environment initialDelegate,
45 InterruptibleSupplier<SkyFunction.Environment> newDelegateSupplier) {
46 this.delegate = initialDelegate;
47 this.newDelegateSupplier = newDelegateSupplier;
Googler1590dbc2023-06-08 15:38:51 -070048 }
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
Googler23e5ec12024-08-30 11:27:18 -070061 // cause anyone holding onto the returned result object to potentially use a stale version
Googler1590dbc2023-06-08 15:38:51 -070062 // 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;
Googler23e5ec12024-08-30 11:27:18 -070068 delegate = newDelegateSupplier.get();
Googler1590dbc2023-06-08 15:38:51 -070069 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;
Googler23e5ec12024-08-30 11:27:18 -0700126 delegate = newDelegateSupplier.get();
Googler1590dbc2023-06-08 15:38:51 -0700127 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
Googler589e2d32024-07-19 11:58:48 -0700153 public boolean inErrorBubbling() {
154 return delegate.inErrorBubbling();
Googler1590dbc2023-06-08 15:38:51 -0700155 }
156
157 @Override
158 public void dependOnFuture(ListenableFuture<?> future) {
159 delegate.dependOnFuture(future);
160 }
161
162 @Override
Googler1590dbc2023-06-08 15:38:51 -0700163 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}