Jakob Buchgraber | 6ec5e87 | 2019-03-29 03:53:17 -0700 | [diff] [blame] | 1 | // Copyright 2019 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 | package com.google.devtools.build.lib.remote; |
| 15 | |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 16 | import build.bazel.remote.execution.v2.Digest; |
George Gensure | 3ef8fb9 | 2020-05-06 09:49:48 -0700 | [diff] [blame] | 17 | import build.bazel.remote.execution.v2.RequestMetadata; |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 18 | import com.google.common.annotations.VisibleForTesting; |
Jakob Buchgraber | 6ec5e87 | 2019-03-29 03:53:17 -0700 | [diff] [blame] | 19 | import com.google.common.base.Preconditions; |
| 20 | import com.google.common.collect.ImmutableSet; |
janakr | c3bcb98 | 2020-04-14 06:50:08 -0700 | [diff] [blame] | 21 | import com.google.common.flogger.GoogleLogger; |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 22 | import com.google.common.util.concurrent.FutureCallback; |
| 23 | import com.google.common.util.concurrent.Futures; |
Jakob Buchgraber | 6ec5e87 | 2019-03-29 03:53:17 -0700 | [diff] [blame] | 24 | import com.google.common.util.concurrent.ListenableFuture; |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 25 | import com.google.common.util.concurrent.MoreExecutors; |
Jakob Buchgraber | 6ec5e87 | 2019-03-29 03:53:17 -0700 | [diff] [blame] | 26 | import com.google.devtools.build.lib.actions.ActionInput; |
| 27 | import com.google.devtools.build.lib.actions.ActionInputPrefetcher; |
| 28 | import com.google.devtools.build.lib.actions.FileArtifactValue; |
| 29 | import com.google.devtools.build.lib.actions.MetadataProvider; |
| 30 | import com.google.devtools.build.lib.actions.cache.VirtualActionInput; |
| 31 | import com.google.devtools.build.lib.profiler.Profiler; |
Jakob Buchgraber | 4b3c2eb | 2019-04-04 02:01:48 -0700 | [diff] [blame] | 32 | import com.google.devtools.build.lib.profiler.ProfilerTask; |
Jakob Buchgraber | 6ec5e87 | 2019-03-29 03:53:17 -0700 | [diff] [blame] | 33 | import com.google.devtools.build.lib.profiler.SilentCloseable; |
Jakob Buchgraber | 1846269 | 2019-11-06 04:34:16 -0800 | [diff] [blame] | 34 | import com.google.devtools.build.lib.remote.common.CacheNotFoundException; |
Jakob Buchgraber | 6ec5e87 | 2019-03-29 03:53:17 -0700 | [diff] [blame] | 35 | import com.google.devtools.build.lib.remote.util.DigestUtil; |
George Gensure | 3ef8fb9 | 2020-05-06 09:49:48 -0700 | [diff] [blame] | 36 | import com.google.devtools.build.lib.remote.util.TracingMetadataUtils; |
Jakob Buchgraber | 6ec5e87 | 2019-03-29 03:53:17 -0700 | [diff] [blame] | 37 | import com.google.devtools.build.lib.vfs.Path; |
| 38 | import io.grpc.Context; |
| 39 | import java.io.IOException; |
| 40 | import java.io.OutputStream; |
| 41 | import java.util.HashMap; |
| 42 | import java.util.HashSet; |
| 43 | import java.util.Map; |
| 44 | import java.util.Set; |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 45 | import java.util.concurrent.ExecutionException; |
Jakob Buchgraber | 6ec5e87 | 2019-03-29 03:53:17 -0700 | [diff] [blame] | 46 | import javax.annotation.concurrent.GuardedBy; |
| 47 | |
| 48 | /** |
| 49 | * Stages output files that are stored remotely to the local filesystem. |
| 50 | * |
| 51 | * <p>This is necessary for remote caching/execution when {@code |
buchgr | d480c5f | 2019-04-03 00:53:34 -0700 | [diff] [blame] | 52 | * --experimental_remote_download_outputs=minimal} is specified. |
Jakob Buchgraber | 6ec5e87 | 2019-03-29 03:53:17 -0700 | [diff] [blame] | 53 | */ |
| 54 | class RemoteActionInputFetcher implements ActionInputPrefetcher { |
| 55 | |
janakr | c3bcb98 | 2020-04-14 06:50:08 -0700 | [diff] [blame] | 56 | private static final GoogleLogger logger = GoogleLogger.forEnclosingClass(); |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 57 | |
Jakob Buchgraber | 6ec5e87 | 2019-03-29 03:53:17 -0700 | [diff] [blame] | 58 | private final Object lock = new Object(); |
| 59 | |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 60 | /** Set of successfully downloaded output files. */ |
Jakob Buchgraber | 6ec5e87 | 2019-03-29 03:53:17 -0700 | [diff] [blame] | 61 | @GuardedBy("lock") |
| 62 | private final Set<Path> downloadedPaths = new HashSet<>(); |
| 63 | |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 64 | @VisibleForTesting |
Jakob Buchgraber | 6ec5e87 | 2019-03-29 03:53:17 -0700 | [diff] [blame] | 65 | @GuardedBy("lock") |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 66 | final Map<Path, ListenableFuture<Void>> downloadsInProgress = new HashMap<>(); |
Jakob Buchgraber | 6ec5e87 | 2019-03-29 03:53:17 -0700 | [diff] [blame] | 67 | |
Jakob Buchgraber | 6056609 | 2019-11-11 06:28:22 -0800 | [diff] [blame] | 68 | private final RemoteCache remoteCache; |
Jakob Buchgraber | 6ec5e87 | 2019-03-29 03:53:17 -0700 | [diff] [blame] | 69 | private final Path execRoot; |
George Gensure | 3ef8fb9 | 2020-05-06 09:49:48 -0700 | [diff] [blame] | 70 | private final RequestMetadata requestMetadata; |
Jakob Buchgraber | 6ec5e87 | 2019-03-29 03:53:17 -0700 | [diff] [blame] | 71 | |
George Gensure | 3ef8fb9 | 2020-05-06 09:49:48 -0700 | [diff] [blame] | 72 | RemoteActionInputFetcher( |
| 73 | RemoteCache remoteCache, Path execRoot, RequestMetadata requestMetadata) { |
Jakob Buchgraber | 6ec5e87 | 2019-03-29 03:53:17 -0700 | [diff] [blame] | 74 | this.remoteCache = Preconditions.checkNotNull(remoteCache); |
| 75 | this.execRoot = Preconditions.checkNotNull(execRoot); |
George Gensure | 3ef8fb9 | 2020-05-06 09:49:48 -0700 | [diff] [blame] | 76 | this.requestMetadata = Preconditions.checkNotNull(requestMetadata); |
Jakob Buchgraber | 6ec5e87 | 2019-03-29 03:53:17 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Fetches remotely stored action outputs, that are inputs to this spawn, and stores them under |
| 81 | * their path in the output base. |
| 82 | * |
| 83 | * <p>This method blocks until all downloads have finished. |
| 84 | * |
| 85 | * <p>This method is safe to be called concurrently from spawn runners before running any local |
| 86 | * spawn. |
| 87 | */ |
| 88 | @Override |
| 89 | public void prefetchFiles( |
| 90 | Iterable<? extends ActionInput> inputs, MetadataProvider metadataProvider) |
| 91 | throws IOException, InterruptedException { |
Jakob Buchgraber | 4b3c2eb | 2019-04-04 02:01:48 -0700 | [diff] [blame] | 92 | try (SilentCloseable c = |
| 93 | Profiler.instance().profile(ProfilerTask.REMOTE_DOWNLOAD, "stage remote inputs")) { |
Jakob Buchgraber | 6ec5e87 | 2019-03-29 03:53:17 -0700 | [diff] [blame] | 94 | Map<Path, ListenableFuture<Void>> downloadsToWaitFor = new HashMap<>(); |
| 95 | for (ActionInput input : inputs) { |
| 96 | if (input instanceof VirtualActionInput) { |
| 97 | VirtualActionInput paramFileActionInput = (VirtualActionInput) input; |
| 98 | Path outputPath = execRoot.getRelative(paramFileActionInput.getExecPath()); |
| 99 | outputPath.getParentDirectory().createDirectoryAndParents(); |
| 100 | try (OutputStream out = outputPath.getOutputStream()) { |
| 101 | paramFileActionInput.writeTo(out); |
| 102 | } |
| 103 | } else { |
| 104 | FileArtifactValue metadata = metadataProvider.getMetadata(input); |
| 105 | if (metadata == null || !metadata.isRemote()) { |
| 106 | continue; |
| 107 | } |
| 108 | |
| 109 | Path path = execRoot.getRelative(input.getExecPath()); |
| 110 | synchronized (lock) { |
| 111 | if (downloadedPaths.contains(path)) { |
| 112 | continue; |
| 113 | } |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 114 | ListenableFuture<Void> download = downloadFileAsync(path, metadata); |
Jakob Buchgraber | 6ec5e87 | 2019-03-29 03:53:17 -0700 | [diff] [blame] | 115 | downloadsToWaitFor.putIfAbsent(path, download); |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
George Gensure | 7cf5a5e | 2020-04-28 08:48:19 -0700 | [diff] [blame] | 120 | try { |
| 121 | RemoteCache.waitForBulkTransfer( |
| 122 | downloadsToWaitFor.values(), /* cancelRemainingOnInterrupt=*/ true); |
| 123 | } catch (BulkTransferException e) { |
| 124 | if (e.onlyCausedByCacheNotFoundException()) { |
| 125 | BulkTransferException bulkAnnotatedException = new BulkTransferException(); |
| 126 | for (Throwable t : e.getSuppressed()) { |
| 127 | IOException annotatedException = |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 128 | new IOException( |
| 129 | String.format( |
| 130 | "Failed to fetch file with hash '%s' because it does not exist remotely." |
utsav-dbx | 79d9ef2 | 2020-06-09 03:44:26 -0700 | [diff] [blame^] | 131 | + " --remote_download_outputs=minimal does not work if" |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 132 | + " your remote cache evicts files during builds.", |
George Gensure | 7cf5a5e | 2020-04-28 08:48:19 -0700 | [diff] [blame] | 133 | ((CacheNotFoundException) t).getMissingDigest().getHash())); |
| 134 | bulkAnnotatedException.add(annotatedException); |
Jakob Buchgraber | 6ec5e87 | 2019-03-29 03:53:17 -0700 | [diff] [blame] | 135 | } |
George Gensure | 7cf5a5e | 2020-04-28 08:48:19 -0700 | [diff] [blame] | 136 | e = bulkAnnotatedException; |
Jakob Buchgraber | 6ec5e87 | 2019-03-29 03:53:17 -0700 | [diff] [blame] | 137 | } |
George Gensure | 7cf5a5e | 2020-04-28 08:48:19 -0700 | [diff] [blame] | 138 | throw e; |
Jakob Buchgraber | 6ec5e87 | 2019-03-29 03:53:17 -0700 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | ImmutableSet<Path> downloadedFiles() { |
| 144 | synchronized (lock) { |
| 145 | return ImmutableSet.copyOf(downloadedPaths); |
| 146 | } |
| 147 | } |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 148 | |
| 149 | void downloadFile(Path path, FileArtifactValue metadata) |
| 150 | throws IOException, InterruptedException { |
| 151 | try { |
| 152 | downloadFileAsync(path, metadata).get(); |
| 153 | } catch (ExecutionException e) { |
| 154 | if (e.getCause() instanceof IOException) { |
| 155 | throw (IOException) e.getCause(); |
| 156 | } |
| 157 | throw new IOException(e.getCause()); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | private ListenableFuture<Void> downloadFileAsync(Path path, FileArtifactValue metadata) |
| 162 | throws IOException { |
| 163 | synchronized (lock) { |
| 164 | if (downloadedPaths.contains(path)) { |
| 165 | return Futures.immediateFuture(null); |
| 166 | } |
| 167 | |
| 168 | ListenableFuture<Void> download = downloadsInProgress.get(path); |
| 169 | if (download == null) { |
George Gensure | 3ef8fb9 | 2020-05-06 09:49:48 -0700 | [diff] [blame] | 170 | Context ctx = |
| 171 | TracingMetadataUtils.contextWithMetadata( |
| 172 | requestMetadata.toBuilder().setActionId(metadata.getActionId()).build()); |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 173 | Context prevCtx = ctx.attach(); |
| 174 | try { |
| 175 | Digest digest = DigestUtil.buildDigest(metadata.getDigest(), metadata.getSize()); |
| 176 | download = remoteCache.downloadFile(path, digest); |
| 177 | downloadsInProgress.put(path, download); |
| 178 | Futures.addCallback( |
| 179 | download, |
| 180 | new FutureCallback<Void>() { |
| 181 | @Override |
| 182 | public void onSuccess(Void v) { |
| 183 | synchronized (lock) { |
| 184 | downloadsInProgress.remove(path); |
| 185 | downloadedPaths.add(path); |
| 186 | } |
| 187 | |
| 188 | try { |
Jakob Buchgraber | c7e8a92 | 2019-06-19 10:04:26 -0700 | [diff] [blame] | 189 | path.chmod(0755); |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 190 | } catch (IOException e) { |
janakr | c3bcb98 | 2020-04-14 06:50:08 -0700 | [diff] [blame] | 191 | logger.atWarning().withCause(e).log("Failed to chmod 755 on %s", path); |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | |
| 195 | @Override |
| 196 | public void onFailure(Throwable t) { |
| 197 | synchronized (lock) { |
| 198 | downloadsInProgress.remove(path); |
| 199 | } |
| 200 | try { |
| 201 | path.delete(); |
| 202 | } catch (IOException e) { |
janakr | c3bcb98 | 2020-04-14 06:50:08 -0700 | [diff] [blame] | 203 | logger.atWarning().withCause(e).log( |
| 204 | "Failed to delete output file after incomplete download: %s", path); |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | }, |
| 208 | MoreExecutors.directExecutor()); |
| 209 | } finally { |
| 210 | ctx.detach(prevCtx); |
| 211 | } |
| 212 | } |
| 213 | return download; |
| 214 | } |
| 215 | } |
Jakob Buchgraber | 6ec5e87 | 2019-03-29 03:53:17 -0700 | [diff] [blame] | 216 | } |