Hadrien Chauvin | 3d0a04d | 2017-12-20 08:45:45 -0800 | [diff] [blame] | 1 | // Copyright 2017 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 | |
jhorvitz | ed7ec3b | 2020-07-24 15:08:03 -0700 | [diff] [blame] | 16 | import static com.google.common.util.concurrent.Futures.immediateFuture; |
Jakob Buchgraber | 584ae24 | 2019-03-29 08:58:41 -0700 | [diff] [blame] | 17 | import static com.google.common.util.concurrent.MoreExecutors.directExecutor; |
Chi Wang | 0f812eb | 2021-07-06 01:14:43 -0700 | [diff] [blame^] | 18 | import static com.google.devtools.build.lib.remote.common.ProgressStatusListener.NO_ACTION; |
| 19 | import static com.google.devtools.build.lib.remote.util.Utils.bytesCountToDisplayString; |
George Gensure | aeee3e0 | 2020-04-15 04:43:45 -0700 | [diff] [blame] | 20 | import static com.google.devtools.build.lib.remote.util.Utils.getFromFuture; |
Jakob Buchgraber | 584ae24 | 2019-03-29 08:58:41 -0700 | [diff] [blame] | 21 | |
olaola | f0aa55d | 2018-08-16 08:51:06 -0700 | [diff] [blame] | 22 | import build.bazel.remote.execution.v2.Action; |
| 23 | import build.bazel.remote.execution.v2.ActionResult; |
| 24 | import build.bazel.remote.execution.v2.Command; |
| 25 | import build.bazel.remote.execution.v2.Digest; |
| 26 | import build.bazel.remote.execution.v2.Directory; |
| 27 | import build.bazel.remote.execution.v2.DirectoryNode; |
| 28 | import build.bazel.remote.execution.v2.FileNode; |
| 29 | import build.bazel.remote.execution.v2.OutputDirectory; |
| 30 | import build.bazel.remote.execution.v2.OutputFile; |
olaola | fbfb3cb | 2018-11-08 11:14:57 -0800 | [diff] [blame] | 31 | import build.bazel.remote.execution.v2.OutputSymlink; |
| 32 | import build.bazel.remote.execution.v2.SymlinkNode; |
olaola | f0aa55d | 2018-08-16 08:51:06 -0700 | [diff] [blame] | 33 | import build.bazel.remote.execution.v2.Tree; |
buchgr | ff008f4 | 2018-06-02 14:13:43 -0700 | [diff] [blame] | 34 | import com.google.common.base.Preconditions; |
Jakob Buchgraber | 584ae24 | 2019-03-29 08:58:41 -0700 | [diff] [blame] | 35 | import com.google.common.collect.ImmutableList; |
| 36 | import com.google.common.collect.ImmutableMap; |
| 37 | import com.google.common.collect.ImmutableSet; |
olaola | fbfb3cb | 2018-11-08 11:14:57 -0800 | [diff] [blame] | 38 | import com.google.common.collect.Iterables; |
Jakob Buchgraber | 584ae24 | 2019-03-29 08:58:41 -0700 | [diff] [blame] | 39 | import com.google.common.collect.Maps; |
Chi Wang | c7c7f5f | 2020-11-09 22:29:35 -0800 | [diff] [blame] | 40 | import com.google.common.flogger.GoogleLogger; |
buchgr | ff008f4 | 2018-06-02 14:13:43 -0700 | [diff] [blame] | 41 | import com.google.common.util.concurrent.FutureCallback; |
| 42 | import com.google.common.util.concurrent.Futures; |
| 43 | import com.google.common.util.concurrent.ListenableFuture; |
Chi Wang | 4ca8946 | 2021-06-10 04:49:49 -0700 | [diff] [blame] | 44 | import com.google.common.util.concurrent.MoreExecutors; |
buchgr | ff008f4 | 2018-06-02 14:13:43 -0700 | [diff] [blame] | 45 | import com.google.common.util.concurrent.SettableFuture; |
Jakob Buchgraber | 584ae24 | 2019-03-29 08:58:41 -0700 | [diff] [blame] | 46 | import com.google.devtools.build.lib.actions.ActionInput; |
| 47 | import com.google.devtools.build.lib.actions.Artifact; |
Googler | 04c546f | 2020-05-12 18:22:18 -0700 | [diff] [blame] | 48 | import com.google.devtools.build.lib.actions.Artifact.SpecialArtifact; |
| 49 | import com.google.devtools.build.lib.actions.Artifact.TreeFileArtifact; |
Hadrien Chauvin | 3d0a04d | 2017-12-20 08:45:45 -0800 | [diff] [blame] | 50 | import com.google.devtools.build.lib.actions.EnvironmentalExecException; |
| 51 | import com.google.devtools.build.lib.actions.ExecException; |
Benjamin Peterson | dd3ddb0 | 2018-05-03 09:20:08 -0700 | [diff] [blame] | 52 | import com.google.devtools.build.lib.actions.UserExecException; |
Jakob Buchgraber | 584ae24 | 2019-03-29 08:58:41 -0700 | [diff] [blame] | 53 | import com.google.devtools.build.lib.actions.cache.MetadataInjector; |
Hadrien Chauvin | 3d0a04d | 2017-12-20 08:45:45 -0800 | [diff] [blame] | 54 | import com.google.devtools.build.lib.concurrent.ThreadSafety; |
Chi Wang | 0f812eb | 2021-07-06 01:14:43 -0700 | [diff] [blame^] | 55 | import com.google.devtools.build.lib.exec.SpawnProgressEvent; |
Jakob Buchgraber | d75b6cf | 2019-06-19 08:12:49 -0700 | [diff] [blame] | 56 | import com.google.devtools.build.lib.exec.SpawnRunner.SpawnExecutionContext; |
Jakob Buchgraber | 584ae24 | 2019-03-29 08:58:41 -0700 | [diff] [blame] | 57 | import com.google.devtools.build.lib.profiler.Profiler; |
| 58 | import com.google.devtools.build.lib.profiler.SilentCloseable; |
Jakob Buchgraber | 6056609 | 2019-11-11 06:28:22 -0800 | [diff] [blame] | 59 | import com.google.devtools.build.lib.remote.RemoteCache.ActionResultMetadata.DirectoryMetadata; |
| 60 | import com.google.devtools.build.lib.remote.RemoteCache.ActionResultMetadata.FileMetadata; |
| 61 | import com.google.devtools.build.lib.remote.RemoteCache.ActionResultMetadata.SymlinkMetadata; |
Johannes Abt | ba5b2a7 | 2021-06-16 23:47:38 -0700 | [diff] [blame] | 62 | import com.google.devtools.build.lib.remote.common.LazyFileOutputStream; |
Chi Wang | 4ca8946 | 2021-06-10 04:49:49 -0700 | [diff] [blame] | 63 | import com.google.devtools.build.lib.remote.common.OutputDigestMismatchException; |
Chi Wang | 0f812eb | 2021-07-06 01:14:43 -0700 | [diff] [blame^] | 64 | import com.google.devtools.build.lib.remote.common.ProgressStatusListener; |
Googler | bc54c64 | 2021-01-26 01:24:39 -0800 | [diff] [blame] | 65 | import com.google.devtools.build.lib.remote.common.RemoteActionExecutionContext; |
Chi Wang | b6e3ba8 | 2021-01-14 21:57:28 -0800 | [diff] [blame] | 66 | import com.google.devtools.build.lib.remote.common.RemoteActionFileArtifactValue; |
Jakob Buchgraber | 6056609 | 2019-11-11 06:28:22 -0800 | [diff] [blame] | 67 | import com.google.devtools.build.lib.remote.common.RemoteCacheClient; |
| 68 | import com.google.devtools.build.lib.remote.common.RemoteCacheClient.ActionKey; |
Chi Wang | b863dbf | 2021-04-19 00:55:59 -0700 | [diff] [blame] | 69 | import com.google.devtools.build.lib.remote.common.RemotePathResolver; |
Jakob Buchgraber | 75b7ed4 | 2019-03-27 10:27:13 -0700 | [diff] [blame] | 70 | import com.google.devtools.build.lib.remote.options.RemoteOptions; |
Googler | 922d1e6 | 2018-03-05 14:49:00 -0800 | [diff] [blame] | 71 | import com.google.devtools.build.lib.remote.util.DigestUtil; |
Jakob Buchgraber | 584ae24 | 2019-03-29 08:58:41 -0700 | [diff] [blame] | 72 | import com.google.devtools.build.lib.remote.util.Utils.InMemoryOutput; |
mschaller | 1eabf52 | 2020-06-10 22:03:13 -0700 | [diff] [blame] | 73 | import com.google.devtools.build.lib.server.FailureDetails.FailureDetail; |
| 74 | import com.google.devtools.build.lib.server.FailureDetails.RemoteExecution; |
| 75 | import com.google.devtools.build.lib.server.FailureDetails.RemoteExecution.Code; |
jhorvitz | ed7ec3b | 2020-07-24 15:08:03 -0700 | [diff] [blame] | 76 | import com.google.devtools.build.lib.skyframe.TreeArtifactValue; |
Hadrien Chauvin | 3d0a04d | 2017-12-20 08:45:45 -0800 | [diff] [blame] | 77 | import com.google.devtools.build.lib.util.io.FileOutErr; |
Jakob Buchgraber | 584ae24 | 2019-03-29 08:58:41 -0700 | [diff] [blame] | 78 | import com.google.devtools.build.lib.util.io.OutErr; |
Hadrien Chauvin | 3d0a04d | 2017-12-20 08:45:45 -0800 | [diff] [blame] | 79 | import com.google.devtools.build.lib.vfs.Dirent; |
Benjamin Peterson | dd3ddb0 | 2018-05-03 09:20:08 -0700 | [diff] [blame] | 80 | import com.google.devtools.build.lib.vfs.FileStatus; |
Hadrien Chauvin | 3d0a04d | 2017-12-20 08:45:45 -0800 | [diff] [blame] | 81 | import com.google.devtools.build.lib.vfs.FileSystemUtils; |
| 82 | import com.google.devtools.build.lib.vfs.Path; |
olaola | fbfb3cb | 2018-11-08 11:14:57 -0800 | [diff] [blame] | 83 | import com.google.devtools.build.lib.vfs.PathFragment; |
Benjamin Peterson | dd3ddb0 | 2018-05-03 09:20:08 -0700 | [diff] [blame] | 84 | import com.google.devtools.build.lib.vfs.Symlinks; |
Jakob Buchgraber | 584ae24 | 2019-03-29 08:58:41 -0700 | [diff] [blame] | 85 | import com.google.protobuf.ByteString; |
| 86 | import com.google.protobuf.InvalidProtocolBufferException; |
buchgr | ff008f4 | 2018-06-02 14:13:43 -0700 | [diff] [blame] | 87 | import java.io.ByteArrayOutputStream; |
Hadrien Chauvin | 3d0a04d | 2017-12-20 08:45:45 -0800 | [diff] [blame] | 88 | import java.io.IOException; |
| 89 | import java.io.OutputStream; |
| 90 | import java.util.ArrayList; |
| 91 | import java.util.Collection; |
Jakob Buchgraber | d75b6cf | 2019-06-19 08:12:49 -0700 | [diff] [blame] | 92 | import java.util.Collections; |
Hadrien Chauvin | 3d0a04d | 2017-12-20 08:45:45 -0800 | [diff] [blame] | 93 | import java.util.Comparator; |
| 94 | import java.util.HashMap; |
| 95 | import java.util.List; |
| 96 | import java.util.Map; |
Jakob Buchgraber | 584ae24 | 2019-03-29 08:58:41 -0700 | [diff] [blame] | 97 | import java.util.Map.Entry; |
Chi Wang | 0f812eb | 2021-07-06 01:14:43 -0700 | [diff] [blame^] | 98 | import java.util.concurrent.atomic.AtomicLong; |
| 99 | import java.util.regex.Matcher; |
| 100 | import java.util.regex.Pattern; |
Jakob Buchgraber | 584ae24 | 2019-03-29 08:58:41 -0700 | [diff] [blame] | 101 | import java.util.stream.Collectors; |
| 102 | import java.util.stream.Stream; |
Hadrien Chauvin | 3d0a04d | 2017-12-20 08:45:45 -0800 | [diff] [blame] | 103 | import javax.annotation.Nullable; |
| 104 | |
| 105 | /** A cache for storing artifacts (input and output) as well as the output of running an action. */ |
| 106 | @ThreadSafety.ThreadSafe |
Jakob Buchgraber | 6056609 | 2019-11-11 06:28:22 -0800 | [diff] [blame] | 107 | public class RemoteCache implements AutoCloseable { |
Chi Wang | c7c7f5f | 2020-11-09 22:29:35 -0800 | [diff] [blame] | 108 | private static final GoogleLogger logger = GoogleLogger.forEnclosingClass(); |
buchgr | ff008f4 | 2018-06-02 14:13:43 -0700 | [diff] [blame] | 109 | |
Jakob Buchgraber | d75b6cf | 2019-06-19 08:12:49 -0700 | [diff] [blame] | 110 | /** See {@link SpawnExecutionContext#lockOutputFiles()}. */ |
| 111 | @FunctionalInterface |
| 112 | interface OutputFilesLocker { |
michajlo | 6bc145b | 2020-10-16 14:41:35 -0700 | [diff] [blame] | 113 | void lock() throws InterruptedException; |
Jakob Buchgraber | d75b6cf | 2019-06-19 08:12:49 -0700 | [diff] [blame] | 114 | } |
| 115 | |
jhorvitz | ed7ec3b | 2020-07-24 15:08:03 -0700 | [diff] [blame] | 116 | private static final ListenableFuture<Void> COMPLETED_SUCCESS = immediateFuture(null); |
| 117 | private static final ListenableFuture<byte[]> EMPTY_BYTES = immediateFuture(new byte[0]); |
buchgr | ff008f4 | 2018-06-02 14:13:43 -0700 | [diff] [blame] | 118 | |
Jakob Buchgraber | 6056609 | 2019-11-11 06:28:22 -0800 | [diff] [blame] | 119 | protected final RemoteCacheClient cacheProtocol; |
Benjamin Peterson | dd3ddb0 | 2018-05-03 09:20:08 -0700 | [diff] [blame] | 120 | protected final RemoteOptions options; |
Hadrien Chauvin | 3d0a04d | 2017-12-20 08:45:45 -0800 | [diff] [blame] | 121 | protected final DigestUtil digestUtil; |
| 122 | |
Chi Wang | 4ca8946 | 2021-06-10 04:49:49 -0700 | [diff] [blame] | 123 | private Path captureCorruptedOutputsDir; |
| 124 | |
Jakob Buchgraber | 6056609 | 2019-11-11 06:28:22 -0800 | [diff] [blame] | 125 | public RemoteCache( |
| 126 | RemoteCacheClient cacheProtocol, RemoteOptions options, DigestUtil digestUtil) { |
| 127 | this.cacheProtocol = cacheProtocol; |
Benjamin Peterson | dd3ddb0 | 2018-05-03 09:20:08 -0700 | [diff] [blame] | 128 | this.options = options; |
Hadrien Chauvin | 3d0a04d | 2017-12-20 08:45:45 -0800 | [diff] [blame] | 129 | this.digestUtil = digestUtil; |
| 130 | } |
| 131 | |
Chi Wang | 4ca8946 | 2021-06-10 04:49:49 -0700 | [diff] [blame] | 132 | public void setCaptureCorruptedOutputsDir(Path captureCorruptedOutputsDir) { |
| 133 | this.captureCorruptedOutputsDir = captureCorruptedOutputsDir; |
| 134 | } |
| 135 | |
Googler | bc54c64 | 2021-01-26 01:24:39 -0800 | [diff] [blame] | 136 | public ActionResult downloadActionResult( |
| 137 | RemoteActionExecutionContext context, ActionKey actionKey, boolean inlineOutErr) |
Jakob Buchgraber | 6056609 | 2019-11-11 06:28:22 -0800 | [diff] [blame] | 138 | throws IOException, InterruptedException { |
Googler | bc54c64 | 2021-01-26 01:24:39 -0800 | [diff] [blame] | 139 | return getFromFuture(cacheProtocol.downloadActionResult(context, actionKey, inlineOutErr)); |
Jakob Buchgraber | 6056609 | 2019-11-11 06:28:22 -0800 | [diff] [blame] | 140 | } |
Jakob Buchgraber | 9fb83b4 | 2019-08-14 06:01:13 -0700 | [diff] [blame] | 141 | |
buchgr | 12ebb84 | 2019-08-21 02:03:24 -0700 | [diff] [blame] | 142 | /** |
| 143 | * Upload the result of a locally executed action to the remote cache. |
| 144 | * |
| 145 | * @throws IOException if there was an error uploading to the remote cache |
| 146 | * @throws ExecException if uploading any of the action outputs is not supported |
| 147 | */ |
| 148 | public ActionResult upload( |
Googler | 92955e6 | 2021-01-27 20:42:16 -0800 | [diff] [blame] | 149 | RemoteActionExecutionContext context, |
Chi Wang | b863dbf | 2021-04-19 00:55:59 -0700 | [diff] [blame] | 150 | RemotePathResolver remotePathResolver, |
buchgr | 12ebb84 | 2019-08-21 02:03:24 -0700 | [diff] [blame] | 151 | ActionKey actionKey, |
| 152 | Action action, |
| 153 | Command command, |
buchgr | 12ebb84 | 2019-08-21 02:03:24 -0700 | [diff] [blame] | 154 | Collection<Path> outputs, |
| 155 | FileOutErr outErr, |
| 156 | int exitCode) |
| 157 | throws ExecException, IOException, InterruptedException { |
| 158 | ActionResult.Builder resultBuilder = ActionResult.newBuilder(); |
Chi Wang | b863dbf | 2021-04-19 00:55:59 -0700 | [diff] [blame] | 159 | uploadOutputs( |
| 160 | context, remotePathResolver, actionKey, action, command, outputs, outErr, resultBuilder); |
buchgr | 12ebb84 | 2019-08-21 02:03:24 -0700 | [diff] [blame] | 161 | resultBuilder.setExitCode(exitCode); |
| 162 | ActionResult result = resultBuilder.build(); |
| 163 | if (exitCode == 0 && !action.getDoNotCache()) { |
Googler | 92955e6 | 2021-01-27 20:42:16 -0800 | [diff] [blame] | 164 | cacheProtocol.uploadActionResult(context, actionKey, result); |
buchgr | 12ebb84 | 2019-08-21 02:03:24 -0700 | [diff] [blame] | 165 | } |
| 166 | return result; |
| 167 | } |
| 168 | |
| 169 | public ActionResult upload( |
Googler | 92955e6 | 2021-01-27 20:42:16 -0800 | [diff] [blame] | 170 | RemoteActionExecutionContext context, |
Chi Wang | b863dbf | 2021-04-19 00:55:59 -0700 | [diff] [blame] | 171 | RemotePathResolver remotePathResolver, |
buchgr | 12ebb84 | 2019-08-21 02:03:24 -0700 | [diff] [blame] | 172 | ActionKey actionKey, |
| 173 | Action action, |
| 174 | Command command, |
buchgr | 12ebb84 | 2019-08-21 02:03:24 -0700 | [diff] [blame] | 175 | Collection<Path> outputs, |
| 176 | FileOutErr outErr) |
| 177 | throws ExecException, IOException, InterruptedException { |
Googler | 92955e6 | 2021-01-27 20:42:16 -0800 | [diff] [blame] | 178 | return upload( |
Chi Wang | b863dbf | 2021-04-19 00:55:59 -0700 | [diff] [blame] | 179 | context, |
| 180 | remotePathResolver, |
| 181 | actionKey, |
| 182 | action, |
| 183 | command, |
| 184 | outputs, |
| 185 | outErr, |
| 186 | /* exitCode= */ 0); |
buchgr | 12ebb84 | 2019-08-21 02:03:24 -0700 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | private void uploadOutputs( |
Googler | 37ee252 | 2021-01-28 22:54:20 -0800 | [diff] [blame] | 190 | RemoteActionExecutionContext context, |
Chi Wang | b863dbf | 2021-04-19 00:55:59 -0700 | [diff] [blame] | 191 | RemotePathResolver remotePathResolver, |
buchgr | 12ebb84 | 2019-08-21 02:03:24 -0700 | [diff] [blame] | 192 | ActionKey actionKey, |
| 193 | Action action, |
| 194 | Command command, |
| 195 | Collection<Path> files, |
| 196 | FileOutErr outErr, |
| 197 | ActionResult.Builder result) |
| 198 | throws ExecException, IOException, InterruptedException { |
| 199 | UploadManifest manifest = |
| 200 | new UploadManifest( |
| 201 | digestUtil, |
Chi Wang | b863dbf | 2021-04-19 00:55:59 -0700 | [diff] [blame] | 202 | remotePathResolver, |
buchgr | 12ebb84 | 2019-08-21 02:03:24 -0700 | [diff] [blame] | 203 | result, |
buchgr | 12ebb84 | 2019-08-21 02:03:24 -0700 | [diff] [blame] | 204 | options.incompatibleRemoteSymlinks, |
| 205 | options.allowSymlinkUpload); |
| 206 | manifest.addFiles(files); |
| 207 | manifest.setStdoutStderr(outErr); |
| 208 | manifest.addAction(actionKey, action, command); |
| 209 | |
| 210 | Map<Digest, Path> digestToFile = manifest.getDigestToFile(); |
| 211 | Map<Digest, ByteString> digestToBlobs = manifest.getDigestToBlobs(); |
| 212 | Collection<Digest> digests = new ArrayList<>(); |
| 213 | digests.addAll(digestToFile.keySet()); |
| 214 | digests.addAll(digestToBlobs.keySet()); |
| 215 | |
Googler | f8d49fa | 2021-01-29 00:03:35 -0800 | [diff] [blame] | 216 | ImmutableSet<Digest> digestsToUpload = |
| 217 | getFromFuture(cacheProtocol.findMissingDigests(context, digests)); |
buchgr | 12ebb84 | 2019-08-21 02:03:24 -0700 | [diff] [blame] | 218 | ImmutableList.Builder<ListenableFuture<Void>> uploads = ImmutableList.builder(); |
| 219 | for (Digest digest : digestsToUpload) { |
| 220 | Path file = digestToFile.get(digest); |
| 221 | if (file != null) { |
Googler | 37ee252 | 2021-01-28 22:54:20 -0800 | [diff] [blame] | 222 | uploads.add(cacheProtocol.uploadFile(context, digest, file)); |
buchgr | 12ebb84 | 2019-08-21 02:03:24 -0700 | [diff] [blame] | 223 | } else { |
| 224 | ByteString blob = digestToBlobs.get(digest); |
| 225 | if (blob == null) { |
| 226 | String message = "FindMissingBlobs call returned an unknown digest: " + digest; |
| 227 | throw new IOException(message); |
| 228 | } |
Googler | 37ee252 | 2021-01-28 22:54:20 -0800 | [diff] [blame] | 229 | uploads.add(cacheProtocol.uploadBlob(context, digest, blob)); |
buchgr | 12ebb84 | 2019-08-21 02:03:24 -0700 | [diff] [blame] | 230 | } |
| 231 | } |
| 232 | |
George Gensure | aeee3e0 | 2020-04-15 04:43:45 -0700 | [diff] [blame] | 233 | waitForBulkTransfer(uploads.build(), /* cancelRemainingOnInterrupt=*/ false); |
buchgr | 12ebb84 | 2019-08-21 02:03:24 -0700 | [diff] [blame] | 234 | |
| 235 | if (manifest.getStderrDigest() != null) { |
| 236 | result.setStderrDigest(manifest.getStderrDigest()); |
| 237 | } |
| 238 | if (manifest.getStdoutDigest() != null) { |
| 239 | result.setStdoutDigest(manifest.getStdoutDigest()); |
| 240 | } |
| 241 | } |
| 242 | |
Ulf Adams | 4f008c5 | 2020-09-17 01:17:59 -0700 | [diff] [blame] | 243 | public static void waitForBulkTransfer( |
| 244 | Iterable<? extends ListenableFuture<?>> transfers, boolean cancelRemainingOnInterrupt) |
George Gensure | aeee3e0 | 2020-04-15 04:43:45 -0700 | [diff] [blame] | 245 | throws BulkTransferException, InterruptedException { |
| 246 | BulkTransferException bulkTransferException = null; |
| 247 | InterruptedException interruptedException = null; |
| 248 | boolean interrupted = Thread.currentThread().isInterrupted(); |
Ulf Adams | 4f008c5 | 2020-09-17 01:17:59 -0700 | [diff] [blame] | 249 | for (ListenableFuture<?> transfer : transfers) { |
George Gensure | aeee3e0 | 2020-04-15 04:43:45 -0700 | [diff] [blame] | 250 | try { |
| 251 | if (interruptedException == null) { |
George Gensure | 24f97e1 | 2020-04-17 05:42:46 -0700 | [diff] [blame] | 252 | // Wait for all transfers to finish. |
Chi Wang | 62e169a | 2020-11-12 00:26:32 -0800 | [diff] [blame] | 253 | getFromFuture(transfer, cancelRemainingOnInterrupt); |
George Gensure | aeee3e0 | 2020-04-15 04:43:45 -0700 | [diff] [blame] | 254 | } else { |
| 255 | transfer.cancel(true); |
| 256 | } |
| 257 | } catch (IOException e) { |
| 258 | if (bulkTransferException == null) { |
| 259 | bulkTransferException = new BulkTransferException(); |
| 260 | } |
| 261 | bulkTransferException.add(e); |
| 262 | } catch (InterruptedException e) { |
| 263 | interrupted = Thread.interrupted() || interrupted; |
| 264 | interruptedException = e; |
| 265 | if (!cancelRemainingOnInterrupt) { |
| 266 | // leave the rest of the transfers alone |
| 267 | break; |
| 268 | } |
buchgr | 12ebb84 | 2019-08-21 02:03:24 -0700 | [diff] [blame] | 269 | } |
George Gensure | aeee3e0 | 2020-04-15 04:43:45 -0700 | [diff] [blame] | 270 | } |
| 271 | if (interrupted) { |
| 272 | Thread.currentThread().interrupt(); |
| 273 | } |
| 274 | if (interruptedException != null) { |
| 275 | if (bulkTransferException != null) { |
| 276 | interruptedException.addSuppressed(bulkTransferException); |
buchgr | 12ebb84 | 2019-08-21 02:03:24 -0700 | [diff] [blame] | 277 | } |
George Gensure | aeee3e0 | 2020-04-15 04:43:45 -0700 | [diff] [blame] | 278 | throw interruptedException; |
| 279 | } |
| 280 | if (bulkTransferException != null) { |
| 281 | throw bulkTransferException; |
buchgr | 12ebb84 | 2019-08-21 02:03:24 -0700 | [diff] [blame] | 282 | } |
| 283 | } |
| 284 | |
Jakob Buchgraber | 9fb83b4 | 2019-08-14 06:01:13 -0700 | [diff] [blame] | 285 | /** |
buchgr | ff008f4 | 2018-06-02 14:13:43 -0700 | [diff] [blame] | 286 | * Downloads a blob with content hash {@code digest} and stores its content in memory. |
Hadrien Chauvin | 3d0a04d | 2017-12-20 08:45:45 -0800 | [diff] [blame] | 287 | * |
buchgr | ff008f4 | 2018-06-02 14:13:43 -0700 | [diff] [blame] | 288 | * @return a future that completes after the download completes (succeeds / fails). If successful, |
| 289 | * the content is stored in the future's {@code byte[]}. |
Hadrien Chauvin | 3d0a04d | 2017-12-20 08:45:45 -0800 | [diff] [blame] | 290 | */ |
Googler | 75bd1ff | 2021-01-27 21:34:14 -0800 | [diff] [blame] | 291 | public ListenableFuture<byte[]> downloadBlob( |
| 292 | RemoteActionExecutionContext context, Digest digest) { |
buchgr | ff008f4 | 2018-06-02 14:13:43 -0700 | [diff] [blame] | 293 | if (digest.getSizeBytes() == 0) { |
| 294 | return EMPTY_BYTES; |
| 295 | } |
| 296 | ByteArrayOutputStream bOut = new ByteArrayOutputStream((int) digest.getSizeBytes()); |
| 297 | SettableFuture<byte[]> outerF = SettableFuture.create(); |
| 298 | Futures.addCallback( |
Googler | 75bd1ff | 2021-01-27 21:34:14 -0800 | [diff] [blame] | 299 | cacheProtocol.downloadBlob(context, digest, bOut), |
buchgr | ff008f4 | 2018-06-02 14:13:43 -0700 | [diff] [blame] | 300 | new FutureCallback<Void>() { |
| 301 | @Override |
| 302 | public void onSuccess(Void aVoid) { |
Chi Wang | c7c7f5f | 2020-11-09 22:29:35 -0800 | [diff] [blame] | 303 | try { |
| 304 | outerF.set(bOut.toByteArray()); |
| 305 | } catch (RuntimeException e) { |
| 306 | logger.atWarning().withCause(e).log("Unexpected exception"); |
| 307 | outerF.setException(e); |
| 308 | } |
buchgr | ff008f4 | 2018-06-02 14:13:43 -0700 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | @Override |
| 312 | public void onFailure(Throwable t) { |
| 313 | outerF.setException(t); |
| 314 | } |
| 315 | }, |
Jakob Buchgraber | 584ae24 | 2019-03-29 08:58:41 -0700 | [diff] [blame] | 316 | directExecutor()); |
buchgr | ff008f4 | 2018-06-02 14:13:43 -0700 | [diff] [blame] | 317 | return outerF; |
| 318 | } |
Hadrien Chauvin | 3d0a04d | 2017-12-20 08:45:45 -0800 | [diff] [blame] | 319 | |
Jakob Buchgraber | d75b6cf | 2019-06-19 08:12:49 -0700 | [diff] [blame] | 320 | private static Path toTmpDownloadPath(Path actualPath) { |
| 321 | return actualPath.getParentDirectory().getRelative(actualPath.getBaseName() + ".tmp"); |
| 322 | } |
| 323 | |
Chi Wang | 0f812eb | 2021-07-06 01:14:43 -0700 | [diff] [blame^] | 324 | static class DownloadProgressReporter { |
| 325 | private static final Pattern PATTERN = Pattern.compile("^bazel-out/[^/]+/[^/]+/"); |
| 326 | private final ProgressStatusListener listener; |
| 327 | private final String id; |
| 328 | private final String file; |
| 329 | private final String totalSize; |
| 330 | private final AtomicLong downloadedBytes = new AtomicLong(0); |
| 331 | |
| 332 | DownloadProgressReporter(ProgressStatusListener listener, String file, long totalSize) { |
| 333 | this.listener = listener; |
| 334 | this.id = file; |
| 335 | this.totalSize = bytesCountToDisplayString(totalSize); |
| 336 | |
| 337 | Matcher matcher = PATTERN.matcher(file); |
| 338 | this.file = matcher.replaceFirst(""); |
| 339 | } |
| 340 | |
| 341 | void started() { |
| 342 | reportProgress(false, false); |
| 343 | } |
| 344 | |
| 345 | void downloadedBytes(int count) { |
| 346 | downloadedBytes.addAndGet(count); |
| 347 | reportProgress(true, false); |
| 348 | } |
| 349 | |
| 350 | void finished() { |
| 351 | reportProgress(true, true); |
| 352 | } |
| 353 | |
| 354 | private void reportProgress(boolean includeBytes, boolean finished) { |
| 355 | String progress; |
| 356 | if (includeBytes) { |
| 357 | progress = |
| 358 | String.format( |
| 359 | "Downloading %s, %s / %s", |
| 360 | file, bytesCountToDisplayString(downloadedBytes.get()), totalSize); |
| 361 | } else { |
| 362 | progress = String.format("Downloading %s", file); |
| 363 | } |
| 364 | listener.onProgressStatus(SpawnProgressEvent.create(id, progress, finished)); |
| 365 | } |
| 366 | } |
| 367 | |
Hadrien Chauvin | 3d0a04d | 2017-12-20 08:45:45 -0800 | [diff] [blame] | 368 | /** |
| 369 | * Download the output files and directory trees of a remotely executed action to the local |
| 370 | * machine, as well stdin / stdout to the given files. |
| 371 | * |
| 372 | * <p>In case of failure, this method deletes any output files it might have already created. |
| 373 | * |
Jakob Buchgraber | d75b6cf | 2019-06-19 08:12:49 -0700 | [diff] [blame] | 374 | * @param outputFilesLocker ensures that we are the only ones writing to the output files when |
| 375 | * using the dynamic spawn strategy. |
Hadrien Chauvin | 3d0a04d | 2017-12-20 08:45:45 -0800 | [diff] [blame] | 376 | * @throws IOException in case of a cache miss or if the remote cache is unavailable. |
| 377 | * @throws ExecException in case clean up after a failed download failed. |
| 378 | */ |
Jakob Buchgraber | d75b6cf | 2019-06-19 08:12:49 -0700 | [diff] [blame] | 379 | public void download( |
Googler | 75bd1ff | 2021-01-27 21:34:14 -0800 | [diff] [blame] | 380 | RemoteActionExecutionContext context, |
Chi Wang | b863dbf | 2021-04-19 00:55:59 -0700 | [diff] [blame] | 381 | RemotePathResolver remotePathResolver, |
Jakob Buchgraber | d75b6cf | 2019-06-19 08:12:49 -0700 | [diff] [blame] | 382 | ActionResult result, |
Jakob Buchgraber | d75b6cf | 2019-06-19 08:12:49 -0700 | [diff] [blame] | 383 | FileOutErr origOutErr, |
Chi Wang | 0f812eb | 2021-07-06 01:14:43 -0700 | [diff] [blame^] | 384 | OutputFilesLocker outputFilesLocker, |
| 385 | ProgressStatusListener progressStatusListener) |
Hadrien Chauvin | 3d0a04d | 2017-12-20 08:45:45 -0800 | [diff] [blame] | 386 | throws ExecException, IOException, InterruptedException { |
Chi Wang | b863dbf | 2021-04-19 00:55:59 -0700 | [diff] [blame] | 387 | ActionResultMetadata metadata = parseActionResultMetadata(context, remotePathResolver, result); |
buchgr | ff008f4 | 2018-06-02 14:13:43 -0700 | [diff] [blame] | 388 | |
Jakob Buchgraber | 584ae24 | 2019-03-29 08:58:41 -0700 | [diff] [blame] | 389 | List<ListenableFuture<FileMetadata>> downloads = |
| 390 | Stream.concat( |
| 391 | metadata.files().stream(), |
| 392 | metadata.directories().stream() |
| 393 | .flatMap((entry) -> entry.getValue().files().stream())) |
| 394 | .map( |
| 395 | (file) -> { |
| 396 | try { |
Jakob Buchgraber | d75b6cf | 2019-06-19 08:12:49 -0700 | [diff] [blame] | 397 | ListenableFuture<Void> download = |
Chi Wang | 4ca8946 | 2021-06-10 04:49:49 -0700 | [diff] [blame] | 398 | downloadFile( |
| 399 | context, |
| 400 | remotePathResolver.localPathToOutputPath(file.path()), |
| 401 | toTmpDownloadPath(file.path()), |
Chi Wang | 0f812eb | 2021-07-06 01:14:43 -0700 | [diff] [blame^] | 402 | file.digest(), |
| 403 | new DownloadProgressReporter( |
| 404 | progressStatusListener, |
| 405 | remotePathResolver.localPathToOutputPath(file.path()), |
| 406 | file.digest().getSizeBytes())); |
Jakob Buchgraber | 584ae24 | 2019-03-29 08:58:41 -0700 | [diff] [blame] | 407 | return Futures.transform(download, (d) -> file, directExecutor()); |
| 408 | } catch (IOException e) { |
| 409 | return Futures.<FileMetadata>immediateFailedFuture(e); |
| 410 | } |
| 411 | }) |
| 412 | .collect(Collectors.toList()); |
buchgr | ff008f4 | 2018-06-02 14:13:43 -0700 | [diff] [blame] | 413 | |
buchgr | bca19128 | 2018-07-25 05:40:42 -0700 | [diff] [blame] | 414 | // Subsequently we need to wait for *every* download to finish, even if we already know that |
| 415 | // one failed. That's so that when exiting this method we can be sure that all downloads have |
| 416 | // finished and don't race with the cleanup routine. |
buchgr | bca19128 | 2018-07-25 05:40:42 -0700 | [diff] [blame] | 417 | |
pcloudy | 130f86d | 2019-04-29 05:55:23 -0700 | [diff] [blame] | 418 | FileOutErr tmpOutErr = null; |
George Gensure | aeee3e0 | 2020-04-15 04:43:45 -0700 | [diff] [blame] | 419 | if (origOutErr != null) { |
| 420 | tmpOutErr = origOutErr.childOutErr(); |
| 421 | } |
Googler | 75bd1ff | 2021-01-27 21:34:14 -0800 | [diff] [blame] | 422 | downloads.addAll(downloadOutErr(context, result, tmpOutErr)); |
George Gensure | aeee3e0 | 2020-04-15 04:43:45 -0700 | [diff] [blame] | 423 | |
buchgr | bca19128 | 2018-07-25 05:40:42 -0700 | [diff] [blame] | 424 | try { |
George Gensure | aeee3e0 | 2020-04-15 04:43:45 -0700 | [diff] [blame] | 425 | waitForBulkTransfer(downloads, /* cancelRemainingOnInterrupt=*/ true); |
| 426 | } catch (Exception e) { |
Chi Wang | 4ca8946 | 2021-06-10 04:49:49 -0700 | [diff] [blame] | 427 | if (captureCorruptedOutputsDir != null) { |
| 428 | if (e instanceof BulkTransferException) { |
| 429 | for (Throwable suppressed : e.getSuppressed()) { |
| 430 | if (suppressed instanceof OutputDigestMismatchException) { |
| 431 | // Capture corrupted outputs |
| 432 | try { |
| 433 | String outputPath = ((OutputDigestMismatchException) suppressed).getOutputPath(); |
| 434 | Path localPath = ((OutputDigestMismatchException) suppressed).getLocalPath(); |
| 435 | Path dst = captureCorruptedOutputsDir.getRelative(outputPath); |
| 436 | dst.createDirectoryAndParents(); |
| 437 | |
| 438 | // Make sure dst is still under captureCorruptedOutputsDir, otherwise |
| 439 | // IllegalArgumentException will be thrown. |
| 440 | dst.relativeTo(captureCorruptedOutputsDir); |
| 441 | |
| 442 | FileSystemUtils.copyFile(localPath, dst); |
| 443 | } catch (Exception ee) { |
| 444 | ee.addSuppressed(ee); |
| 445 | } |
| 446 | } |
| 447 | } |
| 448 | } |
| 449 | } |
| 450 | |
Hadrien Chauvin | 3d0a04d | 2017-12-20 08:45:45 -0800 | [diff] [blame] | 451 | try { |
Jakob Buchgraber | d75b6cf | 2019-06-19 08:12:49 -0700 | [diff] [blame] | 452 | // Delete any (partially) downloaded output files. |
Hadrien Chauvin | 3d0a04d | 2017-12-20 08:45:45 -0800 | [diff] [blame] | 453 | for (OutputFile file : result.getOutputFilesList()) { |
Chi Wang | b863dbf | 2021-04-19 00:55:59 -0700 | [diff] [blame] | 454 | toTmpDownloadPath(remotePathResolver.outputPathToLocalPath(file.getPath())).delete(); |
Hadrien Chauvin | 3d0a04d | 2017-12-20 08:45:45 -0800 | [diff] [blame] | 455 | } |
| 456 | for (OutputDirectory directory : result.getOutputDirectoriesList()) { |
Keith Smiley | 4392ba4 | 2018-12-10 02:39:37 -0800 | [diff] [blame] | 457 | // Only delete the directories below the output directories because the output |
| 458 | // directories will not be re-created |
Chi Wang | b863dbf | 2021-04-19 00:55:59 -0700 | [diff] [blame] | 459 | remotePathResolver.outputPathToLocalPath(directory.getPath()).deleteTreesBelow(); |
Hadrien Chauvin | 3d0a04d | 2017-12-20 08:45:45 -0800 | [diff] [blame] | 460 | } |
pcloudy | 130f86d | 2019-04-29 05:55:23 -0700 | [diff] [blame] | 461 | if (tmpOutErr != null) { |
| 462 | tmpOutErr.clearOut(); |
| 463 | tmpOutErr.clearErr(); |
Hadrien Chauvin | 3d0a04d | 2017-12-20 08:45:45 -0800 | [diff] [blame] | 464 | } |
George Gensure | aeee3e0 | 2020-04-15 04:43:45 -0700 | [diff] [blame] | 465 | } catch (IOException ioEx) { |
| 466 | ioEx.addSuppressed(e); |
Jakob Buchgraber | 4dc78a0 | 2019-09-26 07:53:11 -0700 | [diff] [blame] | 467 | |
Hadrien Chauvin | 3d0a04d | 2017-12-20 08:45:45 -0800 | [diff] [blame] | 468 | // If deleting of output files failed, we abort the build with a decent error message as |
| 469 | // any subsequent local execution failure would likely be incomprehensible. |
George Gensure | aeee3e0 | 2020-04-15 04:43:45 -0700 | [diff] [blame] | 470 | ExecException execEx = |
| 471 | new EnvironmentalExecException( |
mschaller | 1eabf52 | 2020-06-10 22:03:13 -0700 | [diff] [blame] | 472 | ioEx, |
mschaller | 0793388 | 2020-06-24 14:38:23 -0700 | [diff] [blame] | 473 | createFailureDetail( |
| 474 | "Failed to delete output files after incomplete download", |
| 475 | Code.INCOMPLETE_OUTPUT_DOWNLOAD_CLEANUP_FAILURE)); |
George Gensure | aeee3e0 | 2020-04-15 04:43:45 -0700 | [diff] [blame] | 476 | execEx.addSuppressed(e); |
| 477 | throw execEx; |
Hadrien Chauvin | 3d0a04d | 2017-12-20 08:45:45 -0800 | [diff] [blame] | 478 | } |
George Gensure | aeee3e0 | 2020-04-15 04:43:45 -0700 | [diff] [blame] | 479 | throw e; |
Hadrien Chauvin | 3d0a04d | 2017-12-20 08:45:45 -0800 | [diff] [blame] | 480 | } |
olaola | fbfb3cb | 2018-11-08 11:14:57 -0800 | [diff] [blame] | 481 | |
pcloudy | 130f86d | 2019-04-29 05:55:23 -0700 | [diff] [blame] | 482 | if (tmpOutErr != null) { |
| 483 | FileOutErr.dump(tmpOutErr, origOutErr); |
| 484 | tmpOutErr.clearOut(); |
| 485 | tmpOutErr.clearErr(); |
| 486 | } |
| 487 | |
Jakob Buchgraber | d75b6cf | 2019-06-19 08:12:49 -0700 | [diff] [blame] | 488 | // Ensure that we are the only ones writing to the output files when using the dynamic spawn |
| 489 | // strategy. |
| 490 | outputFilesLocker.lock(); |
| 491 | |
| 492 | moveOutputsToFinalLocation(downloads); |
| 493 | |
Jakob Buchgraber | 584ae24 | 2019-03-29 08:58:41 -0700 | [diff] [blame] | 494 | List<SymlinkMetadata> symlinksInDirectories = new ArrayList<>(); |
| 495 | for (Entry<Path, DirectoryMetadata> entry : metadata.directories()) { |
| 496 | entry.getKey().createDirectoryAndParents(); |
| 497 | symlinksInDirectories.addAll(entry.getValue().symlinks()); |
| 498 | } |
| 499 | |
| 500 | Iterable<SymlinkMetadata> symlinks = |
| 501 | Iterables.concat(metadata.symlinks(), symlinksInDirectories); |
| 502 | |
| 503 | // Create the symbolic links after all downloads are finished, because dangling symlinks |
| 504 | // might not be supported on all platforms |
| 505 | createSymlinks(symlinks); |
olaola | fbfb3cb | 2018-11-08 11:14:57 -0800 | [diff] [blame] | 506 | } |
| 507 | |
Jakob Buchgraber | d75b6cf | 2019-06-19 08:12:49 -0700 | [diff] [blame] | 508 | /** |
| 509 | * Copies moves the downloaded outputs from their download location to their declared location. |
| 510 | */ |
| 511 | private void moveOutputsToFinalLocation(List<ListenableFuture<FileMetadata>> downloads) |
| 512 | throws IOException, InterruptedException { |
| 513 | List<FileMetadata> finishedDownloads = new ArrayList<>(downloads.size()); |
| 514 | for (ListenableFuture<FileMetadata> finishedDownload : downloads) { |
| 515 | FileMetadata outputFile = getFromFuture(finishedDownload); |
| 516 | if (outputFile != null) { |
| 517 | finishedDownloads.add(outputFile); |
| 518 | } |
| 519 | } |
| 520 | /* |
| 521 | * Sort the list lexicographically based on its temporary download path in order to avoid |
| 522 | * filename clashes when moving the files: |
| 523 | * |
| 524 | * Consider an action that produces two outputs foo and foo.tmp. These outputs would initially |
| 525 | * be downloaded to foo.tmp and foo.tmp.tmp. When renaming them to foo and foo.tmp we need to |
| 526 | * ensure that rename(foo.tmp, foo) happens before rename(foo.tmp.tmp, foo.tmp). We ensure this |
| 527 | * by doing the renames in lexicographical order of the download names. |
| 528 | */ |
| 529 | Collections.sort(finishedDownloads, Comparator.comparing(f -> toTmpDownloadPath(f.path()))); |
| 530 | |
| 531 | // Move the output files from their temporary name to the actual output file name. |
| 532 | for (FileMetadata outputFile : finishedDownloads) { |
| 533 | FileSystemUtils.moveFile(toTmpDownloadPath(outputFile.path()), outputFile.path()); |
| 534 | outputFile.path().setExecutable(outputFile.isExecutable()); |
| 535 | } |
| 536 | } |
| 537 | |
Jakob Buchgraber | 584ae24 | 2019-03-29 08:58:41 -0700 | [diff] [blame] | 538 | private void createSymlinks(Iterable<SymlinkMetadata> symlinks) throws IOException { |
| 539 | for (SymlinkMetadata symlink : symlinks) { |
| 540 | if (symlink.target().isAbsolute()) { |
| 541 | // We do not support absolute symlinks as outputs. |
Hadrien Chauvin | 3d0a04d | 2017-12-20 08:45:45 -0800 | [diff] [blame] | 542 | throw new IOException( |
Jakob Buchgraber | 584ae24 | 2019-03-29 08:58:41 -0700 | [diff] [blame] | 543 | String.format( |
| 544 | "Action output %s is a symbolic link to an absolute path %s. " |
| 545 | + "Symlinks to absolute paths in action outputs are not supported.", |
| 546 | symlink.path(), symlink.target())); |
Hadrien Chauvin | 3d0a04d | 2017-12-20 08:45:45 -0800 | [diff] [blame] | 547 | } |
Jakob Buchgraber | 584ae24 | 2019-03-29 08:58:41 -0700 | [diff] [blame] | 548 | Preconditions.checkNotNull( |
| 549 | symlink.path().getParentDirectory(), |
| 550 | "Failed creating directory and parents for %s", |
| 551 | symlink.path()) |
| 552 | .createDirectoryAndParents(); |
| 553 | symlink.path().createSymbolicLink(symlink.target()); |
Hadrien Chauvin | 3d0a04d | 2017-12-20 08:45:45 -0800 | [diff] [blame] | 554 | } |
| 555 | } |
| 556 | |
Chi Wang | 4ca8946 | 2021-06-10 04:49:49 -0700 | [diff] [blame] | 557 | public ListenableFuture<Void> downloadFile( |
Chi Wang | 0f812eb | 2021-07-06 01:14:43 -0700 | [diff] [blame^] | 558 | RemoteActionExecutionContext context, |
| 559 | String outputPath, |
| 560 | Path localPath, |
| 561 | Digest digest, |
| 562 | DownloadProgressReporter reporter) |
Chi Wang | 4ca8946 | 2021-06-10 04:49:49 -0700 | [diff] [blame] | 563 | throws IOException { |
| 564 | SettableFuture<Void> outerF = SettableFuture.create(); |
Chi Wang | 0f812eb | 2021-07-06 01:14:43 -0700 | [diff] [blame^] | 565 | ListenableFuture<Void> f = downloadFile(context, localPath, digest, reporter); |
Chi Wang | 4ca8946 | 2021-06-10 04:49:49 -0700 | [diff] [blame] | 566 | Futures.addCallback( |
| 567 | f, |
| 568 | new FutureCallback<Void>() { |
| 569 | @Override |
| 570 | public void onSuccess(Void unused) { |
| 571 | outerF.set(null); |
| 572 | } |
| 573 | |
| 574 | @Override |
| 575 | public void onFailure(Throwable throwable) { |
| 576 | if (throwable instanceof OutputDigestMismatchException) { |
| 577 | OutputDigestMismatchException e = ((OutputDigestMismatchException) throwable); |
| 578 | e.setOutputPath(outputPath); |
| 579 | e.setLocalPath(localPath); |
| 580 | } |
| 581 | outerF.setException(throwable); |
| 582 | } |
| 583 | }, |
| 584 | MoreExecutors.directExecutor()); |
| 585 | |
| 586 | return outerF; |
| 587 | } |
| 588 | |
jhorvitz | ed7ec3b | 2020-07-24 15:08:03 -0700 | [diff] [blame] | 589 | /** Downloads a file (that is not a directory). The content is fetched from the digest. */ |
Googler | 75bd1ff | 2021-01-27 21:34:14 -0800 | [diff] [blame] | 590 | public ListenableFuture<Void> downloadFile( |
| 591 | RemoteActionExecutionContext context, Path path, Digest digest) throws IOException { |
Chi Wang | 0f812eb | 2021-07-06 01:14:43 -0700 | [diff] [blame^] | 592 | return downloadFile(context, path, digest, new DownloadProgressReporter(NO_ACTION, "", 0)); |
| 593 | } |
| 594 | |
| 595 | /** Downloads a file (that is not a directory). The content is fetched from the digest. */ |
| 596 | public ListenableFuture<Void> downloadFile( |
| 597 | RemoteActionExecutionContext context, |
| 598 | Path path, |
| 599 | Digest digest, |
| 600 | DownloadProgressReporter reporter) |
| 601 | throws IOException { |
buchgr | ff008f4 | 2018-06-02 14:13:43 -0700 | [diff] [blame] | 602 | Preconditions.checkNotNull(path.getParentDirectory()).createDirectoryAndParents(); |
Hadrien Chauvin | 3d0a04d | 2017-12-20 08:45:45 -0800 | [diff] [blame] | 603 | if (digest.getSizeBytes() == 0) { |
| 604 | // Handle empty file locally. |
| 605 | FileSystemUtils.writeContent(path, new byte[0]); |
buchgr | ff008f4 | 2018-06-02 14:13:43 -0700 | [diff] [blame] | 606 | return COMPLETED_SUCCESS; |
Hadrien Chauvin | 3d0a04d | 2017-12-20 08:45:45 -0800 | [diff] [blame] | 607 | } |
buchgr | ff008f4 | 2018-06-02 14:13:43 -0700 | [diff] [blame] | 608 | |
Ed Schouten | cb08ffc | 2020-09-09 23:32:01 -0700 | [diff] [blame] | 609 | if (!options.remoteDownloadSymlinkTemplate.isEmpty()) { |
| 610 | // Don't actually download files from the CAS. Instead, create a |
| 611 | // symbolic link that points to a location where CAS objects may |
| 612 | // be found. This could, for example, be a FUSE file system. |
| 613 | path.createSymbolicLink( |
| 614 | path.getRelative( |
| 615 | options |
| 616 | .remoteDownloadSymlinkTemplate |
| 617 | .replace("{hash}", digest.getHash()) |
| 618 | .replace("{size_bytes}", String.valueOf(digest.getSizeBytes())))); |
| 619 | return COMPLETED_SUCCESS; |
| 620 | } |
| 621 | |
Chi Wang | 0f812eb | 2021-07-06 01:14:43 -0700 | [diff] [blame^] | 622 | reporter.started(); |
| 623 | OutputStream out = new ReportingOutputStream(new LazyFileOutputStream(path), reporter); |
| 624 | |
buchgr | ff008f4 | 2018-06-02 14:13:43 -0700 | [diff] [blame] | 625 | SettableFuture<Void> outerF = SettableFuture.create(); |
Googler | 75bd1ff | 2021-01-27 21:34:14 -0800 | [diff] [blame] | 626 | ListenableFuture<Void> f = cacheProtocol.downloadBlob(context, digest, out); |
buchgr | ff008f4 | 2018-06-02 14:13:43 -0700 | [diff] [blame] | 627 | Futures.addCallback( |
| 628 | f, |
| 629 | new FutureCallback<Void>() { |
| 630 | @Override |
| 631 | public void onSuccess(Void result) { |
| 632 | try { |
| 633 | out.close(); |
| 634 | outerF.set(null); |
Chi Wang | 0f812eb | 2021-07-06 01:14:43 -0700 | [diff] [blame^] | 635 | reporter.finished(); |
buchgr | ff008f4 | 2018-06-02 14:13:43 -0700 | [diff] [blame] | 636 | } catch (IOException e) { |
| 637 | outerF.setException(e); |
Chi Wang | c7c7f5f | 2020-11-09 22:29:35 -0800 | [diff] [blame] | 638 | } catch (RuntimeException e) { |
| 639 | logger.atWarning().withCause(e).log("Unexpected exception"); |
| 640 | outerF.setException(e); |
buchgr | ff008f4 | 2018-06-02 14:13:43 -0700 | [diff] [blame] | 641 | } |
| 642 | } |
| 643 | |
| 644 | @Override |
| 645 | public void onFailure(Throwable t) { |
buchgr | ff008f4 | 2018-06-02 14:13:43 -0700 | [diff] [blame] | 646 | try { |
| 647 | out.close(); |
Chi Wang | 0f812eb | 2021-07-06 01:14:43 -0700 | [diff] [blame^] | 648 | reporter.finished(); |
buchgr | ff008f4 | 2018-06-02 14:13:43 -0700 | [diff] [blame] | 649 | } catch (IOException e) { |
Jakob Buchgraber | 4dc78a0 | 2019-09-26 07:53:11 -0700 | [diff] [blame] | 650 | if (t != e) { |
| 651 | t.addSuppressed(e); |
| 652 | } |
Chi Wang | c7c7f5f | 2020-11-09 22:29:35 -0800 | [diff] [blame] | 653 | } catch (RuntimeException e) { |
| 654 | logger.atWarning().withCause(e).log("Unexpected exception"); |
| 655 | t.addSuppressed(e); |
pcloudy | a3a5975 | 2019-01-07 03:48:37 -0800 | [diff] [blame] | 656 | } finally { |
| 657 | outerF.setException(t); |
buchgr | ff008f4 | 2018-06-02 14:13:43 -0700 | [diff] [blame] | 658 | } |
| 659 | } |
| 660 | }, |
Jakob Buchgraber | 584ae24 | 2019-03-29 08:58:41 -0700 | [diff] [blame] | 661 | directExecutor()); |
buchgr | ff008f4 | 2018-06-02 14:13:43 -0700 | [diff] [blame] | 662 | return outerF; |
Hadrien Chauvin | 3d0a04d | 2017-12-20 08:45:45 -0800 | [diff] [blame] | 663 | } |
| 664 | |
Googler | 75bd1ff | 2021-01-27 21:34:14 -0800 | [diff] [blame] | 665 | private List<ListenableFuture<FileMetadata>> downloadOutErr( |
| 666 | RemoteActionExecutionContext context, ActionResult result, OutErr outErr) { |
Jakob Buchgraber | 584ae24 | 2019-03-29 08:58:41 -0700 | [diff] [blame] | 667 | List<ListenableFuture<FileMetadata>> downloads = new ArrayList<>(); |
Hadrien Chauvin | 3d0a04d | 2017-12-20 08:45:45 -0800 | [diff] [blame] | 668 | if (!result.getStdoutRaw().isEmpty()) { |
George Gensure | aeee3e0 | 2020-04-15 04:43:45 -0700 | [diff] [blame] | 669 | try { |
| 670 | result.getStdoutRaw().writeTo(outErr.getOutputStream()); |
| 671 | outErr.getOutputStream().flush(); |
| 672 | } catch (IOException e) { |
| 673 | downloads.add(Futures.immediateFailedFuture(e)); |
| 674 | } |
Hadrien Chauvin | 3d0a04d | 2017-12-20 08:45:45 -0800 | [diff] [blame] | 675 | } else if (result.hasStdoutDigest()) { |
buchgr | ff008f4 | 2018-06-02 14:13:43 -0700 | [diff] [blame] | 676 | downloads.add( |
Jakob Buchgraber | 584ae24 | 2019-03-29 08:58:41 -0700 | [diff] [blame] | 677 | Futures.transform( |
Googler | 75bd1ff | 2021-01-27 21:34:14 -0800 | [diff] [blame] | 678 | cacheProtocol.downloadBlob( |
| 679 | context, result.getStdoutDigest(), outErr.getOutputStream()), |
Jakob Buchgraber | 584ae24 | 2019-03-29 08:58:41 -0700 | [diff] [blame] | 680 | (d) -> null, |
| 681 | directExecutor())); |
Hadrien Chauvin | 3d0a04d | 2017-12-20 08:45:45 -0800 | [diff] [blame] | 682 | } |
| 683 | if (!result.getStderrRaw().isEmpty()) { |
George Gensure | aeee3e0 | 2020-04-15 04:43:45 -0700 | [diff] [blame] | 684 | try { |
| 685 | result.getStderrRaw().writeTo(outErr.getErrorStream()); |
| 686 | outErr.getErrorStream().flush(); |
| 687 | } catch (IOException e) { |
| 688 | downloads.add(Futures.immediateFailedFuture(e)); |
| 689 | } |
Hadrien Chauvin | 3d0a04d | 2017-12-20 08:45:45 -0800 | [diff] [blame] | 690 | } else if (result.hasStderrDigest()) { |
buchgr | ff008f4 | 2018-06-02 14:13:43 -0700 | [diff] [blame] | 691 | downloads.add( |
Jakob Buchgraber | 584ae24 | 2019-03-29 08:58:41 -0700 | [diff] [blame] | 692 | Futures.transform( |
Googler | 75bd1ff | 2021-01-27 21:34:14 -0800 | [diff] [blame] | 693 | cacheProtocol.downloadBlob( |
| 694 | context, result.getStderrDigest(), outErr.getErrorStream()), |
Jakob Buchgraber | 584ae24 | 2019-03-29 08:58:41 -0700 | [diff] [blame] | 695 | (d) -> null, |
| 696 | directExecutor())); |
Hadrien Chauvin | 3d0a04d | 2017-12-20 08:45:45 -0800 | [diff] [blame] | 697 | } |
buchgr | ff008f4 | 2018-06-02 14:13:43 -0700 | [diff] [blame] | 698 | return downloads; |
Hadrien Chauvin | 3d0a04d | 2017-12-20 08:45:45 -0800 | [diff] [blame] | 699 | } |
| 700 | |
Jakob Buchgraber | 584ae24 | 2019-03-29 08:58:41 -0700 | [diff] [blame] | 701 | /** |
| 702 | * Avoids downloading the majority of action outputs but injects their metadata using {@link |
| 703 | * MetadataInjector} instead. |
| 704 | * |
| 705 | * <p>This method only downloads output directory metadata, stdout and stderr as well as the |
| 706 | * contents of {@code inMemoryOutputPath} if specified. |
| 707 | * |
Googler | 75bd1ff | 2021-01-27 21:34:14 -0800 | [diff] [blame] | 708 | * @param context the context this action running with |
Jakob Buchgraber | 584ae24 | 2019-03-29 08:58:41 -0700 | [diff] [blame] | 709 | * @param result the action result metadata of a successfully executed action (exit code = 0). |
| 710 | * @param outputs the action's declared output files |
| 711 | * @param inMemoryOutputPath the path of an output file whose contents should be returned in |
| 712 | * memory by this method. |
| 713 | * @param outErr stdout and stderr of this action |
Jakob Buchgraber | 584ae24 | 2019-03-29 08:58:41 -0700 | [diff] [blame] | 714 | * @param metadataInjector the action's metadata injector that allows this method to inject |
| 715 | * metadata about an action output instead of downloading the output |
Jakob Buchgraber | d75b6cf | 2019-06-19 08:12:49 -0700 | [diff] [blame] | 716 | * @param outputFilesLocker ensures that we are the only ones writing to the output files when |
| 717 | * using the dynamic spawn strategy. |
Jakob Buchgraber | 584ae24 | 2019-03-29 08:58:41 -0700 | [diff] [blame] | 718 | * @throws IOException in case of failure |
| 719 | * @throws InterruptedException in case of receiving an interrupt |
| 720 | */ |
| 721 | @Nullable |
| 722 | public InMemoryOutput downloadMinimal( |
Googler | 75bd1ff | 2021-01-27 21:34:14 -0800 | [diff] [blame] | 723 | RemoteActionExecutionContext context, |
Chi Wang | b863dbf | 2021-04-19 00:55:59 -0700 | [diff] [blame] | 724 | RemotePathResolver remotePathResolver, |
Jakob Buchgraber | 584ae24 | 2019-03-29 08:58:41 -0700 | [diff] [blame] | 725 | ActionResult result, |
| 726 | Collection<? extends ActionInput> outputs, |
| 727 | @Nullable PathFragment inMemoryOutputPath, |
| 728 | OutErr outErr, |
Jakob Buchgraber | d75b6cf | 2019-06-19 08:12:49 -0700 | [diff] [blame] | 729 | MetadataInjector metadataInjector, |
| 730 | OutputFilesLocker outputFilesLocker) |
Jakob Buchgraber | 584ae24 | 2019-03-29 08:58:41 -0700 | [diff] [blame] | 731 | throws IOException, InterruptedException { |
| 732 | Preconditions.checkState( |
| 733 | result.getExitCode() == 0, |
| 734 | "injecting remote metadata is only supported for successful actions (exit code 0)."); |
| 735 | |
| 736 | ActionResultMetadata metadata; |
| 737 | try (SilentCloseable c = Profiler.instance().profile("Remote.parseActionResultMetadata")) { |
Chi Wang | b863dbf | 2021-04-19 00:55:59 -0700 | [diff] [blame] | 738 | metadata = parseActionResultMetadata(context, remotePathResolver, result); |
Jakob Buchgraber | 584ae24 | 2019-03-29 08:58:41 -0700 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | if (!metadata.symlinks().isEmpty()) { |
| 742 | throw new IOException( |
| 743 | "Symlinks in action outputs are not yet supported by " |
buchgr | d480c5f | 2019-04-03 00:53:34 -0700 | [diff] [blame] | 744 | + "--experimental_remote_download_outputs=minimal"); |
Jakob Buchgraber | 584ae24 | 2019-03-29 08:58:41 -0700 | [diff] [blame] | 745 | } |
| 746 | |
Jakob Buchgraber | d75b6cf | 2019-06-19 08:12:49 -0700 | [diff] [blame] | 747 | // Ensure that when using dynamic spawn strategy that we are the only ones writing to the |
| 748 | // output files. |
| 749 | outputFilesLocker.lock(); |
| 750 | |
Jakob Buchgraber | 584ae24 | 2019-03-29 08:58:41 -0700 | [diff] [blame] | 751 | ActionInput inMemoryOutput = null; |
| 752 | Digest inMemoryOutputDigest = null; |
| 753 | for (ActionInput output : outputs) { |
| 754 | if (inMemoryOutputPath != null && output.getExecPath().equals(inMemoryOutputPath)) { |
Chi Wang | b863dbf | 2021-04-19 00:55:59 -0700 | [diff] [blame] | 755 | Path localPath = remotePathResolver.outputPathToLocalPath(output); |
| 756 | FileMetadata m = metadata.file(localPath); |
Jakob Buchgraber | e05dca3 | 2020-04-28 04:29:27 -0700 | [diff] [blame] | 757 | if (m == null) { |
| 758 | // A declared output wasn't created. Ignore it here. SkyFrame will fail if not all |
| 759 | // outputs were created. |
| 760 | continue; |
| 761 | } |
Jakob Buchgraber | 584ae24 | 2019-03-29 08:58:41 -0700 | [diff] [blame] | 762 | inMemoryOutputDigest = m.digest(); |
| 763 | inMemoryOutput = output; |
| 764 | } |
| 765 | if (output instanceof Artifact) { |
Chi Wang | b863dbf | 2021-04-19 00:55:59 -0700 | [diff] [blame] | 766 | injectRemoteArtifact( |
| 767 | context, remotePathResolver, (Artifact) output, metadata, metadataInjector); |
Jakob Buchgraber | 584ae24 | 2019-03-29 08:58:41 -0700 | [diff] [blame] | 768 | } |
| 769 | } |
| 770 | |
| 771 | try (SilentCloseable c = Profiler.instance().profile("Remote.download")) { |
| 772 | ListenableFuture<byte[]> inMemoryOutputDownload = null; |
| 773 | if (inMemoryOutput != null) { |
Googler | 75bd1ff | 2021-01-27 21:34:14 -0800 | [diff] [blame] | 774 | inMemoryOutputDownload = downloadBlob(context, inMemoryOutputDigest); |
Jakob Buchgraber | 584ae24 | 2019-03-29 08:58:41 -0700 | [diff] [blame] | 775 | } |
Googler | 75bd1ff | 2021-01-27 21:34:14 -0800 | [diff] [blame] | 776 | waitForBulkTransfer( |
| 777 | downloadOutErr(context, result, outErr), /* cancelRemainingOnInterrupt=*/ true); |
Jakob Buchgraber | 584ae24 | 2019-03-29 08:58:41 -0700 | [diff] [blame] | 778 | if (inMemoryOutputDownload != null) { |
George Gensure | 7cf5a5e | 2020-04-28 08:48:19 -0700 | [diff] [blame] | 779 | waitForBulkTransfer( |
| 780 | ImmutableList.of(inMemoryOutputDownload), /* cancelRemainingOnInterrupt=*/ true); |
Jakob Buchgraber | 584ae24 | 2019-03-29 08:58:41 -0700 | [diff] [blame] | 781 | byte[] data = getFromFuture(inMemoryOutputDownload); |
| 782 | return new InMemoryOutput(inMemoryOutput, ByteString.copyFrom(data)); |
| 783 | } |
| 784 | } |
| 785 | return null; |
| 786 | } |
| 787 | |
| 788 | private void injectRemoteArtifact( |
Chi Wang | b863dbf | 2021-04-19 00:55:59 -0700 | [diff] [blame] | 789 | RemoteActionExecutionContext context, |
| 790 | RemotePathResolver remotePathResolver, |
Jakob Buchgraber | 584ae24 | 2019-03-29 08:58:41 -0700 | [diff] [blame] | 791 | Artifact output, |
| 792 | ActionResultMetadata metadata, |
Chi Wang | b863dbf | 2021-04-19 00:55:59 -0700 | [diff] [blame] | 793 | MetadataInjector metadataInjector) |
Jakob Buchgraber | 584ae24 | 2019-03-29 08:58:41 -0700 | [diff] [blame] | 794 | throws IOException { |
Chi Wang | b863dbf | 2021-04-19 00:55:59 -0700 | [diff] [blame] | 795 | Path path = remotePathResolver.outputPathToLocalPath(output); |
Jakob Buchgraber | 584ae24 | 2019-03-29 08:58:41 -0700 | [diff] [blame] | 796 | if (output.isTreeArtifact()) { |
Chi Wang | b863dbf | 2021-04-19 00:55:59 -0700 | [diff] [blame] | 797 | DirectoryMetadata directory = metadata.directory(path); |
Jakob Buchgraber | 584ae24 | 2019-03-29 08:58:41 -0700 | [diff] [blame] | 798 | if (directory == null) { |
| 799 | // A declared output wasn't created. It might have been an optional output and if not |
| 800 | // SkyFrame will make sure to fail. |
| 801 | return; |
| 802 | } |
| 803 | if (!directory.symlinks().isEmpty()) { |
| 804 | throw new IOException( |
| 805 | "Symlinks in action outputs are not yet supported by " |
buchgr | d480c5f | 2019-04-03 00:53:34 -0700 | [diff] [blame] | 806 | + "--experimental_remote_download_outputs=minimal"); |
Jakob Buchgraber | 584ae24 | 2019-03-29 08:58:41 -0700 | [diff] [blame] | 807 | } |
Googler | 04c546f | 2020-05-12 18:22:18 -0700 | [diff] [blame] | 808 | SpecialArtifact parent = (SpecialArtifact) output; |
jhorvitz | ed7ec3b | 2020-07-24 15:08:03 -0700 | [diff] [blame] | 809 | TreeArtifactValue.Builder tree = TreeArtifactValue.newBuilder(parent); |
Jakob Buchgraber | 584ae24 | 2019-03-29 08:58:41 -0700 | [diff] [blame] | 810 | for (FileMetadata file : directory.files()) { |
Googler | 04c546f | 2020-05-12 18:22:18 -0700 | [diff] [blame] | 811 | TreeFileArtifact child = |
Googler | 1d8d138 | 2020-05-18 12:10:49 -0700 | [diff] [blame] | 812 | TreeFileArtifact.createTreeOutput(parent, file.path().relativeTo(parent.getPath())); |
Chi Wang | b6e3ba8 | 2021-01-14 21:57:28 -0800 | [diff] [blame] | 813 | RemoteActionFileArtifactValue value = |
| 814 | new RemoteActionFileArtifactValue( |
Jakob Buchgraber | 584ae24 | 2019-03-29 08:58:41 -0700 | [diff] [blame] | 815 | DigestUtil.toBinaryDigest(file.digest()), |
| 816 | file.digest().getSizeBytes(), |
Googler | 04c546f | 2020-05-12 18:22:18 -0700 | [diff] [blame] | 817 | /*locationIndex=*/ 1, |
Chi Wang | b863dbf | 2021-04-19 00:55:59 -0700 | [diff] [blame] | 818 | context.getRequestMetadata().getActionId(), |
Chi Wang | b6e3ba8 | 2021-01-14 21:57:28 -0800 | [diff] [blame] | 819 | file.isExecutable()); |
jhorvitz | ed7ec3b | 2020-07-24 15:08:03 -0700 | [diff] [blame] | 820 | tree.putChild(child, value); |
Jakob Buchgraber | 584ae24 | 2019-03-29 08:58:41 -0700 | [diff] [blame] | 821 | } |
jhorvitz | ed7ec3b | 2020-07-24 15:08:03 -0700 | [diff] [blame] | 822 | metadataInjector.injectTree(parent, tree.build()); |
Jakob Buchgraber | 584ae24 | 2019-03-29 08:58:41 -0700 | [diff] [blame] | 823 | } else { |
Chi Wang | b863dbf | 2021-04-19 00:55:59 -0700 | [diff] [blame] | 824 | FileMetadata outputMetadata = metadata.file(path); |
Jakob Buchgraber | 584ae24 | 2019-03-29 08:58:41 -0700 | [diff] [blame] | 825 | if (outputMetadata == null) { |
| 826 | // A declared output wasn't created. It might have been an optional output and if not |
| 827 | // SkyFrame will make sure to fail. |
| 828 | return; |
| 829 | } |
Googler | ef55446 | 2020-06-04 17:40:55 -0700 | [diff] [blame] | 830 | metadataInjector.injectFile( |
Jakob Buchgraber | 584ae24 | 2019-03-29 08:58:41 -0700 | [diff] [blame] | 831 | output, |
Chi Wang | b6e3ba8 | 2021-01-14 21:57:28 -0800 | [diff] [blame] | 832 | new RemoteActionFileArtifactValue( |
Googler | b67aaba | 2020-05-13 09:49:37 -0700 | [diff] [blame] | 833 | DigestUtil.toBinaryDigest(outputMetadata.digest()), |
| 834 | outputMetadata.digest().getSizeBytes(), |
| 835 | /*locationIndex=*/ 1, |
Chi Wang | b863dbf | 2021-04-19 00:55:59 -0700 | [diff] [blame] | 836 | context.getRequestMetadata().getActionId(), |
Chi Wang | b6e3ba8 | 2021-01-14 21:57:28 -0800 | [diff] [blame] | 837 | outputMetadata.isExecutable())); |
Jakob Buchgraber | 584ae24 | 2019-03-29 08:58:41 -0700 | [diff] [blame] | 838 | } |
| 839 | } |
| 840 | |
| 841 | private DirectoryMetadata parseDirectory( |
| 842 | Path parent, Directory dir, Map<Digest, Directory> childDirectoriesMap) { |
| 843 | ImmutableList.Builder<FileMetadata> filesBuilder = ImmutableList.builder(); |
| 844 | for (FileNode file : dir.getFilesList()) { |
| 845 | filesBuilder.add( |
| 846 | new FileMetadata( |
| 847 | parent.getRelative(file.getName()), file.getDigest(), file.getIsExecutable())); |
| 848 | } |
| 849 | |
| 850 | ImmutableList.Builder<SymlinkMetadata> symlinksBuilder = ImmutableList.builder(); |
| 851 | for (SymlinkNode symlink : dir.getSymlinksList()) { |
| 852 | symlinksBuilder.add( |
| 853 | new SymlinkMetadata( |
| 854 | parent.getRelative(symlink.getName()), PathFragment.create(symlink.getTarget()))); |
| 855 | } |
| 856 | |
| 857 | for (DirectoryNode directoryNode : dir.getDirectoriesList()) { |
| 858 | Path childPath = parent.getRelative(directoryNode.getName()); |
| 859 | Directory childDir = |
| 860 | Preconditions.checkNotNull(childDirectoriesMap.get(directoryNode.getDigest())); |
| 861 | DirectoryMetadata childMetadata = parseDirectory(childPath, childDir, childDirectoriesMap); |
| 862 | filesBuilder.addAll(childMetadata.files()); |
| 863 | symlinksBuilder.addAll(childMetadata.symlinks()); |
| 864 | } |
| 865 | |
| 866 | return new DirectoryMetadata(filesBuilder.build(), symlinksBuilder.build()); |
| 867 | } |
| 868 | |
Googler | 75bd1ff | 2021-01-27 21:34:14 -0800 | [diff] [blame] | 869 | private ActionResultMetadata parseActionResultMetadata( |
Chi Wang | b863dbf | 2021-04-19 00:55:59 -0700 | [diff] [blame] | 870 | RemoteActionExecutionContext context, |
| 871 | RemotePathResolver remotePathResolver, |
| 872 | ActionResult actionResult) |
Jakob Buchgraber | 584ae24 | 2019-03-29 08:58:41 -0700 | [diff] [blame] | 873 | throws IOException, InterruptedException { |
| 874 | Preconditions.checkNotNull(actionResult, "actionResult"); |
| 875 | Map<Path, ListenableFuture<Tree>> dirMetadataDownloads = |
| 876 | Maps.newHashMapWithExpectedSize(actionResult.getOutputDirectoriesCount()); |
| 877 | for (OutputDirectory dir : actionResult.getOutputDirectoriesList()) { |
| 878 | dirMetadataDownloads.put( |
Chi Wang | b863dbf | 2021-04-19 00:55:59 -0700 | [diff] [blame] | 879 | remotePathResolver.outputPathToLocalPath(dir.getPath()), |
Jakob Buchgraber | 584ae24 | 2019-03-29 08:58:41 -0700 | [diff] [blame] | 880 | Futures.transform( |
Googler | 75bd1ff | 2021-01-27 21:34:14 -0800 | [diff] [blame] | 881 | downloadBlob(context, dir.getTreeDigest()), |
Jakob Buchgraber | 584ae24 | 2019-03-29 08:58:41 -0700 | [diff] [blame] | 882 | (treeBytes) -> { |
| 883 | try { |
| 884 | return Tree.parseFrom(treeBytes); |
| 885 | } catch (InvalidProtocolBufferException e) { |
| 886 | throw new RuntimeException(e); |
| 887 | } |
| 888 | }, |
| 889 | directExecutor())); |
| 890 | } |
| 891 | |
George Gensure | 24f97e1 | 2020-04-17 05:42:46 -0700 | [diff] [blame] | 892 | waitForBulkTransfer(dirMetadataDownloads.values(), /* cancelRemainingOnInterrupt=*/ true); |
| 893 | |
Jakob Buchgraber | 584ae24 | 2019-03-29 08:58:41 -0700 | [diff] [blame] | 894 | ImmutableMap.Builder<Path, DirectoryMetadata> directories = ImmutableMap.builder(); |
| 895 | for (Map.Entry<Path, ListenableFuture<Tree>> metadataDownload : |
| 896 | dirMetadataDownloads.entrySet()) { |
| 897 | Path path = metadataDownload.getKey(); |
| 898 | Tree directoryTree = getFromFuture(metadataDownload.getValue()); |
| 899 | Map<Digest, Directory> childrenMap = new HashMap<>(); |
| 900 | for (Directory childDir : directoryTree.getChildrenList()) { |
| 901 | childrenMap.put(digestUtil.compute(childDir), childDir); |
| 902 | } |
| 903 | |
| 904 | directories.put(path, parseDirectory(path, directoryTree.getRoot(), childrenMap)); |
| 905 | } |
| 906 | |
| 907 | ImmutableMap.Builder<Path, FileMetadata> files = ImmutableMap.builder(); |
| 908 | for (OutputFile outputFile : actionResult.getOutputFilesList()) { |
Chi Wang | b863dbf | 2021-04-19 00:55:59 -0700 | [diff] [blame] | 909 | Path localPath = remotePathResolver.outputPathToLocalPath(outputFile.getPath()); |
Jakob Buchgraber | 584ae24 | 2019-03-29 08:58:41 -0700 | [diff] [blame] | 910 | files.put( |
Chi Wang | b863dbf | 2021-04-19 00:55:59 -0700 | [diff] [blame] | 911 | localPath, |
| 912 | new FileMetadata(localPath, outputFile.getDigest(), outputFile.getIsExecutable())); |
Jakob Buchgraber | 584ae24 | 2019-03-29 08:58:41 -0700 | [diff] [blame] | 913 | } |
| 914 | |
| 915 | ImmutableMap.Builder<Path, SymlinkMetadata> symlinks = ImmutableMap.builder(); |
| 916 | Iterable<OutputSymlink> outputSymlinks = |
| 917 | Iterables.concat( |
| 918 | actionResult.getOutputFileSymlinksList(), |
| 919 | actionResult.getOutputDirectorySymlinksList()); |
| 920 | for (OutputSymlink symlink : outputSymlinks) { |
Chi Wang | b863dbf | 2021-04-19 00:55:59 -0700 | [diff] [blame] | 921 | Path localPath = remotePathResolver.outputPathToLocalPath(symlink.getPath()); |
Jakob Buchgraber | 584ae24 | 2019-03-29 08:58:41 -0700 | [diff] [blame] | 922 | symlinks.put( |
Chi Wang | b863dbf | 2021-04-19 00:55:59 -0700 | [diff] [blame] | 923 | localPath, new SymlinkMetadata(localPath, PathFragment.create(symlink.getTarget()))); |
Jakob Buchgraber | 584ae24 | 2019-03-29 08:58:41 -0700 | [diff] [blame] | 924 | } |
| 925 | |
| 926 | return new ActionResultMetadata(files.build(), symlinks.build(), directories.build()); |
| 927 | } |
| 928 | |
Benjamin Peterson | dd3ddb0 | 2018-05-03 09:20:08 -0700 | [diff] [blame] | 929 | /** UploadManifest adds output metadata to a {@link ActionResult}. */ |
| 930 | static class UploadManifest { |
| 931 | private final DigestUtil digestUtil; |
Chi Wang | b863dbf | 2021-04-19 00:55:59 -0700 | [diff] [blame] | 932 | private final RemotePathResolver remotePathResolver; |
Hadrien Chauvin | 3d0a04d | 2017-12-20 08:45:45 -0800 | [diff] [blame] | 933 | private final ActionResult.Builder result; |
Benjamin Peterson | dd3ddb0 | 2018-05-03 09:20:08 -0700 | [diff] [blame] | 934 | private final boolean allowSymlinks; |
olaola | fbfb3cb | 2018-11-08 11:14:57 -0800 | [diff] [blame] | 935 | private final boolean uploadSymlinks; |
buchgr | 59b989e | 2019-08-06 01:36:23 -0700 | [diff] [blame] | 936 | private final Map<Digest, Path> digestToFile = new HashMap<>(); |
Jakob Buchgraber | 9fb83b4 | 2019-08-14 06:01:13 -0700 | [diff] [blame] | 937 | private final Map<Digest, ByteString> digestToBlobs = new HashMap<>(); |
buchgr | 59b989e | 2019-08-06 01:36:23 -0700 | [diff] [blame] | 938 | private Digest stderrDigest; |
| 939 | private Digest stdoutDigest; |
Hadrien Chauvin | 3d0a04d | 2017-12-20 08:45:45 -0800 | [diff] [blame] | 940 | |
| 941 | /** |
| 942 | * Create an UploadManifest from an ActionResult builder and an exec root. The ActionResult |
| 943 | * builder is populated through a call to {@link #addFile(Digest, Path)}. |
| 944 | */ |
Benjamin Peterson | dd3ddb0 | 2018-05-03 09:20:08 -0700 | [diff] [blame] | 945 | public UploadManifest( |
olaola | fbfb3cb | 2018-11-08 11:14:57 -0800 | [diff] [blame] | 946 | DigestUtil digestUtil, |
Chi Wang | b863dbf | 2021-04-19 00:55:59 -0700 | [diff] [blame] | 947 | RemotePathResolver remotePathResolver, |
olaola | fbfb3cb | 2018-11-08 11:14:57 -0800 | [diff] [blame] | 948 | ActionResult.Builder result, |
olaola | fbfb3cb | 2018-11-08 11:14:57 -0800 | [diff] [blame] | 949 | boolean uploadSymlinks, |
| 950 | boolean allowSymlinks) { |
Benjamin Peterson | dd3ddb0 | 2018-05-03 09:20:08 -0700 | [diff] [blame] | 951 | this.digestUtil = digestUtil; |
Chi Wang | b863dbf | 2021-04-19 00:55:59 -0700 | [diff] [blame] | 952 | this.remotePathResolver = remotePathResolver; |
Hadrien Chauvin | 3d0a04d | 2017-12-20 08:45:45 -0800 | [diff] [blame] | 953 | this.result = result; |
olaola | fbfb3cb | 2018-11-08 11:14:57 -0800 | [diff] [blame] | 954 | this.uploadSymlinks = uploadSymlinks; |
Benjamin Peterson | dd3ddb0 | 2018-05-03 09:20:08 -0700 | [diff] [blame] | 955 | this.allowSymlinks = allowSymlinks; |
buchgr | 59b989e | 2019-08-06 01:36:23 -0700 | [diff] [blame] | 956 | } |
Hadrien Chauvin | 3d0a04d | 2017-12-20 08:45:45 -0800 | [diff] [blame] | 957 | |
buchgr | 59b989e | 2019-08-06 01:36:23 -0700 | [diff] [blame] | 958 | public void setStdoutStderr(FileOutErr outErr) throws IOException { |
| 959 | if (outErr.getErrorPath().exists()) { |
| 960 | stderrDigest = digestUtil.compute(outErr.getErrorPath()); |
Jakob Buchgraber | b7d300c | 2019-08-06 04:25:24 -0700 | [diff] [blame] | 961 | digestToFile.put(stderrDigest, outErr.getErrorPath()); |
buchgr | 59b989e | 2019-08-06 01:36:23 -0700 | [diff] [blame] | 962 | } |
| 963 | if (outErr.getOutputPath().exists()) { |
| 964 | stdoutDigest = digestUtil.compute(outErr.getOutputPath()); |
Jakob Buchgraber | b7d300c | 2019-08-06 04:25:24 -0700 | [diff] [blame] | 965 | digestToFile.put(stdoutDigest, outErr.getOutputPath()); |
buchgr | 59b989e | 2019-08-06 01:36:23 -0700 | [diff] [blame] | 966 | } |
Hadrien Chauvin | 3d0a04d | 2017-12-20 08:45:45 -0800 | [diff] [blame] | 967 | } |
| 968 | |
| 969 | /** |
Benjamin Peterson | dd3ddb0 | 2018-05-03 09:20:08 -0700 | [diff] [blame] | 970 | * Add a collection of files or directories to the UploadManifest. Adding a directory has the |
Hadrien Chauvin | 3d0a04d | 2017-12-20 08:45:45 -0800 | [diff] [blame] | 971 | * effect of 1) uploading a {@link Tree} protobuf message from which the whole structure of the |
| 972 | * directory, including the descendants, can be reconstructed and 2) uploading all the |
| 973 | * non-directory descendant files. |
| 974 | */ |
buchgr | ff008f4 | 2018-06-02 14:13:43 -0700 | [diff] [blame] | 975 | public void addFiles(Collection<Path> files) throws ExecException, IOException { |
Hadrien Chauvin | 3d0a04d | 2017-12-20 08:45:45 -0800 | [diff] [blame] | 976 | for (Path file : files) { |
| 977 | // TODO(ulfjack): Maybe pass in a SpawnResult here, add a list of output files to that, and |
| 978 | // rely on the local spawn runner to stat the files, instead of statting here. |
Benjamin Peterson | dd3ddb0 | 2018-05-03 09:20:08 -0700 | [diff] [blame] | 979 | FileStatus stat = file.statIfFound(Symlinks.NOFOLLOW); |
olaola | fbfb3cb | 2018-11-08 11:14:57 -0800 | [diff] [blame] | 980 | // TODO(#6547): handle the case where the parent directory of the output file is an |
| 981 | // output symlink. |
Benjamin Peterson | dd3ddb0 | 2018-05-03 09:20:08 -0700 | [diff] [blame] | 982 | if (stat == null) { |
Hadrien Chauvin | 3d0a04d | 2017-12-20 08:45:45 -0800 | [diff] [blame] | 983 | // We ignore requested results that have not been generated by the action. |
| 984 | continue; |
| 985 | } |
Benjamin Peterson | dd3ddb0 | 2018-05-03 09:20:08 -0700 | [diff] [blame] | 986 | if (stat.isDirectory()) { |
Hadrien Chauvin | 3d0a04d | 2017-12-20 08:45:45 -0800 | [diff] [blame] | 987 | addDirectory(file); |
olaola | fbfb3cb | 2018-11-08 11:14:57 -0800 | [diff] [blame] | 988 | } else if (stat.isFile() && !stat.isSpecialFile()) { |
Benjamin Peterson | dd3ddb0 | 2018-05-03 09:20:08 -0700 | [diff] [blame] | 989 | Digest digest = digestUtil.compute(file, stat.getSize()); |
buchgr | fa36d2f | 2018-04-18 04:41:10 -0700 | [diff] [blame] | 990 | addFile(digest, file); |
olaola | fbfb3cb | 2018-11-08 11:14:57 -0800 | [diff] [blame] | 991 | } else if (stat.isSymbolicLink() && allowSymlinks) { |
| 992 | PathFragment target = file.readSymbolicLink(); |
| 993 | // Need to resolve the symbolic link to know what to add, file or directory. |
| 994 | FileStatus statFollow = file.statIfFound(Symlinks.FOLLOW); |
| 995 | if (statFollow == null) { |
| 996 | throw new IOException( |
| 997 | String.format("Action output %s is a dangling symbolic link to %s ", file, target)); |
| 998 | } |
| 999 | if (statFollow.isSpecialFile()) { |
| 1000 | illegalOutput(file); |
| 1001 | } |
| 1002 | Preconditions.checkState( |
| 1003 | statFollow.isFile() || statFollow.isDirectory(), "Unknown stat type for %s", file); |
| 1004 | if (uploadSymlinks && !target.isAbsolute()) { |
| 1005 | if (statFollow.isFile()) { |
| 1006 | addFileSymbolicLink(file, target); |
| 1007 | } else { |
| 1008 | addDirectorySymbolicLink(file, target); |
| 1009 | } |
| 1010 | } else { |
| 1011 | if (statFollow.isFile()) { |
| 1012 | addFile(digestUtil.compute(file), file); |
| 1013 | } else { |
| 1014 | addDirectory(file); |
| 1015 | } |
| 1016 | } |
Benjamin Peterson | dd3ddb0 | 2018-05-03 09:20:08 -0700 | [diff] [blame] | 1017 | } else { |
| 1018 | illegalOutput(file); |
Hadrien Chauvin | 3d0a04d | 2017-12-20 08:45:45 -0800 | [diff] [blame] | 1019 | } |
| 1020 | } |
| 1021 | } |
| 1022 | |
olaola | f0aa55d | 2018-08-16 08:51:06 -0700 | [diff] [blame] | 1023 | /** |
| 1024 | * Adds an action and command protos to upload. They need to be uploaded as part of the action |
| 1025 | * result. |
| 1026 | */ |
Jakob Buchgraber | 6056609 | 2019-11-11 06:28:22 -0800 | [diff] [blame] | 1027 | public void addAction(RemoteCacheClient.ActionKey actionKey, Action action, Command command) { |
Jakob Buchgraber | 9fb83b4 | 2019-08-14 06:01:13 -0700 | [diff] [blame] | 1028 | digestToBlobs.put(actionKey.getDigest(), action.toByteString()); |
| 1029 | digestToBlobs.put(action.getCommandDigest(), command.toByteString()); |
olaola | f0aa55d | 2018-08-16 08:51:06 -0700 | [diff] [blame] | 1030 | } |
| 1031 | |
Hadrien Chauvin | 3d0a04d | 2017-12-20 08:45:45 -0800 | [diff] [blame] | 1032 | /** Map of digests to file paths to upload. */ |
| 1033 | public Map<Digest, Path> getDigestToFile() { |
| 1034 | return digestToFile; |
| 1035 | } |
| 1036 | |
| 1037 | /** |
| 1038 | * Map of digests to chunkers to upload. When the file is a regular, non-directory file it is |
| 1039 | * transmitted through {@link #getDigestToFile()}. When it is a directory, it is transmitted as |
Jakob Buchgraber | 9fb83b4 | 2019-08-14 06:01:13 -0700 | [diff] [blame] | 1040 | * a {@link Tree} protobuf message through {@link #getDigestToBlobs()}. |
Hadrien Chauvin | 3d0a04d | 2017-12-20 08:45:45 -0800 | [diff] [blame] | 1041 | */ |
Jakob Buchgraber | 9fb83b4 | 2019-08-14 06:01:13 -0700 | [diff] [blame] | 1042 | public Map<Digest, ByteString> getDigestToBlobs() { |
| 1043 | return digestToBlobs; |
Hadrien Chauvin | 3d0a04d | 2017-12-20 08:45:45 -0800 | [diff] [blame] | 1044 | } |
| 1045 | |
buchgr | 59b989e | 2019-08-06 01:36:23 -0700 | [diff] [blame] | 1046 | @Nullable |
| 1047 | public Digest getStdoutDigest() { |
| 1048 | return stdoutDigest; |
| 1049 | } |
| 1050 | |
| 1051 | @Nullable |
| 1052 | public Digest getStderrDigest() { |
| 1053 | return stderrDigest; |
| 1054 | } |
| 1055 | |
olaola | fbfb3cb | 2018-11-08 11:14:57 -0800 | [diff] [blame] | 1056 | private void addFileSymbolicLink(Path file, PathFragment target) throws IOException { |
| 1057 | result |
| 1058 | .addOutputFileSymlinksBuilder() |
Chi Wang | b863dbf | 2021-04-19 00:55:59 -0700 | [diff] [blame] | 1059 | .setPath(remotePathResolver.localPathToOutputPath(file)) |
olaola | fbfb3cb | 2018-11-08 11:14:57 -0800 | [diff] [blame] | 1060 | .setTarget(target.toString()); |
| 1061 | } |
| 1062 | |
| 1063 | private void addDirectorySymbolicLink(Path file, PathFragment target) throws IOException { |
| 1064 | result |
| 1065 | .addOutputDirectorySymlinksBuilder() |
Chi Wang | b863dbf | 2021-04-19 00:55:59 -0700 | [diff] [blame] | 1066 | .setPath(remotePathResolver.localPathToOutputPath(file)) |
olaola | fbfb3cb | 2018-11-08 11:14:57 -0800 | [diff] [blame] | 1067 | .setTarget(target.toString()); |
| 1068 | } |
| 1069 | |
Hadrien Chauvin | 3d0a04d | 2017-12-20 08:45:45 -0800 | [diff] [blame] | 1070 | private void addFile(Digest digest, Path file) throws IOException { |
| 1071 | result |
| 1072 | .addOutputFilesBuilder() |
Chi Wang | b863dbf | 2021-04-19 00:55:59 -0700 | [diff] [blame] | 1073 | .setPath(remotePathResolver.localPathToOutputPath(file)) |
Hadrien Chauvin | 3d0a04d | 2017-12-20 08:45:45 -0800 | [diff] [blame] | 1074 | .setDigest(digest) |
| 1075 | .setIsExecutable(file.isExecutable()); |
| 1076 | |
| 1077 | digestToFile.put(digest, file); |
| 1078 | } |
| 1079 | |
Benjamin Peterson | dd3ddb0 | 2018-05-03 09:20:08 -0700 | [diff] [blame] | 1080 | private void addDirectory(Path dir) throws ExecException, IOException { |
Hadrien Chauvin | 3d0a04d | 2017-12-20 08:45:45 -0800 | [diff] [blame] | 1081 | Tree.Builder tree = Tree.newBuilder(); |
| 1082 | Directory root = computeDirectory(dir, tree); |
| 1083 | tree.setRoot(root); |
| 1084 | |
Jakob Buchgraber | 9fb83b4 | 2019-08-14 06:01:13 -0700 | [diff] [blame] | 1085 | ByteString data = tree.build().toByteString(); |
| 1086 | Digest digest = digestUtil.compute(data.toByteArray()); |
Hadrien Chauvin | 3d0a04d | 2017-12-20 08:45:45 -0800 | [diff] [blame] | 1087 | |
| 1088 | if (result != null) { |
| 1089 | result |
| 1090 | .addOutputDirectoriesBuilder() |
Chi Wang | b863dbf | 2021-04-19 00:55:59 -0700 | [diff] [blame] | 1091 | .setPath(remotePathResolver.localPathToOutputPath(dir)) |
Hadrien Chauvin | 3d0a04d | 2017-12-20 08:45:45 -0800 | [diff] [blame] | 1092 | .setTreeDigest(digest); |
| 1093 | } |
| 1094 | |
Jakob Buchgraber | 9fb83b4 | 2019-08-14 06:01:13 -0700 | [diff] [blame] | 1095 | digestToBlobs.put(digest, data); |
Hadrien Chauvin | 3d0a04d | 2017-12-20 08:45:45 -0800 | [diff] [blame] | 1096 | } |
| 1097 | |
Benjamin Peterson | dd3ddb0 | 2018-05-03 09:20:08 -0700 | [diff] [blame] | 1098 | private Directory computeDirectory(Path path, Tree.Builder tree) |
| 1099 | throws ExecException, IOException { |
Hadrien Chauvin | 3d0a04d | 2017-12-20 08:45:45 -0800 | [diff] [blame] | 1100 | Directory.Builder b = Directory.newBuilder(); |
| 1101 | |
olaola | fbfb3cb | 2018-11-08 11:14:57 -0800 | [diff] [blame] | 1102 | List<Dirent> sortedDirent = new ArrayList<>(path.readdir(Symlinks.NOFOLLOW)); |
Hadrien Chauvin | 3d0a04d | 2017-12-20 08:45:45 -0800 | [diff] [blame] | 1103 | sortedDirent.sort(Comparator.comparing(Dirent::getName)); |
| 1104 | |
| 1105 | for (Dirent dirent : sortedDirent) { |
| 1106 | String name = dirent.getName(); |
| 1107 | Path child = path.getRelative(name); |
| 1108 | if (dirent.getType() == Dirent.Type.DIRECTORY) { |
| 1109 | Directory dir = computeDirectory(child, tree); |
| 1110 | b.addDirectoriesBuilder().setName(name).setDigest(digestUtil.compute(dir)); |
| 1111 | tree.addChildren(dir); |
olaola | fbfb3cb | 2018-11-08 11:14:57 -0800 | [diff] [blame] | 1112 | } else if (dirent.getType() == Dirent.Type.SYMLINK && allowSymlinks) { |
| 1113 | PathFragment target = child.readSymbolicLink(); |
| 1114 | if (uploadSymlinks && !target.isAbsolute()) { |
| 1115 | // Whether it is dangling or not, we're passing it on. |
| 1116 | b.addSymlinksBuilder().setName(name).setTarget(target.toString()); |
| 1117 | continue; |
| 1118 | } |
| 1119 | // Need to resolve the symbolic link now to know whether to upload a file or a directory. |
| 1120 | FileStatus statFollow = child.statIfFound(Symlinks.FOLLOW); |
| 1121 | if (statFollow == null) { |
| 1122 | throw new IOException( |
| 1123 | String.format( |
| 1124 | "Action output %s is a dangling symbolic link to %s ", child, target)); |
| 1125 | } |
| 1126 | if (statFollow.isFile() && !statFollow.isSpecialFile()) { |
| 1127 | Digest digest = digestUtil.compute(child); |
| 1128 | b.addFilesBuilder() |
| 1129 | .setName(name) |
| 1130 | .setDigest(digest) |
| 1131 | .setIsExecutable(child.isExecutable()); |
| 1132 | digestToFile.put(digest, child); |
| 1133 | } else if (statFollow.isDirectory()) { |
| 1134 | Directory dir = computeDirectory(child, tree); |
| 1135 | b.addDirectoriesBuilder().setName(name).setDigest(digestUtil.compute(dir)); |
| 1136 | tree.addChildren(dir); |
| 1137 | } else { |
| 1138 | illegalOutput(child); |
| 1139 | } |
| 1140 | } else if (dirent.getType() == Dirent.Type.FILE) { |
Hadrien Chauvin | 3d0a04d | 2017-12-20 08:45:45 -0800 | [diff] [blame] | 1141 | Digest digest = digestUtil.compute(child); |
| 1142 | b.addFilesBuilder().setName(name).setDigest(digest).setIsExecutable(child.isExecutable()); |
| 1143 | digestToFile.put(digest, child); |
Benjamin Peterson | dd3ddb0 | 2018-05-03 09:20:08 -0700 | [diff] [blame] | 1144 | } else { |
| 1145 | illegalOutput(child); |
Hadrien Chauvin | 3d0a04d | 2017-12-20 08:45:45 -0800 | [diff] [blame] | 1146 | } |
| 1147 | } |
| 1148 | |
| 1149 | return b.build(); |
| 1150 | } |
Benjamin Peterson | dd3ddb0 | 2018-05-03 09:20:08 -0700 | [diff] [blame] | 1151 | |
Jakob Buchgraber | 584ae24 | 2019-03-29 08:58:41 -0700 | [diff] [blame] | 1152 | private void illegalOutput(Path what) throws ExecException { |
Benjamin Peterson | dd3ddb0 | 2018-05-03 09:20:08 -0700 | [diff] [blame] | 1153 | String kind = what.isSymbolicLink() ? "symbolic link" : "special file"; |
mschaller | 0793388 | 2020-06-24 14:38:23 -0700 | [diff] [blame] | 1154 | String message = |
Benjamin Peterson | dd3ddb0 | 2018-05-03 09:20:08 -0700 | [diff] [blame] | 1155 | String.format( |
| 1156 | "Output %s is a %s. Only regular files and directories may be " |
| 1157 | + "uploaded to a remote cache. " |
| 1158 | + "Change the file type or use --remote_allow_symlink_upload.", |
Chi Wang | b863dbf | 2021-04-19 00:55:59 -0700 | [diff] [blame] | 1159 | remotePathResolver.localPathToOutputPath(what), kind); |
mschaller | 0793388 | 2020-06-24 14:38:23 -0700 | [diff] [blame] | 1160 | throw new UserExecException(createFailureDetail(message, Code.ILLEGAL_OUTPUT)); |
Benjamin Peterson | dd3ddb0 | 2018-05-03 09:20:08 -0700 | [diff] [blame] | 1161 | } |
Hadrien Chauvin | 3d0a04d | 2017-12-20 08:45:45 -0800 | [diff] [blame] | 1162 | } |
| 1163 | |
| 1164 | /** Release resources associated with the cache. The cache may not be used after calling this. */ |
| 1165 | @Override |
Jakob Buchgraber | 6056609 | 2019-11-11 06:28:22 -0800 | [diff] [blame] | 1166 | public void close() { |
| 1167 | cacheProtocol.close(); |
| 1168 | } |
buchgr | ff008f4 | 2018-06-02 14:13:43 -0700 | [diff] [blame] | 1169 | |
mschaller | 0793388 | 2020-06-24 14:38:23 -0700 | [diff] [blame] | 1170 | private static FailureDetail createFailureDetail(String message, Code detailedCode) { |
| 1171 | return FailureDetail.newBuilder() |
| 1172 | .setMessage(message) |
| 1173 | .setRemoteExecution(RemoteExecution.newBuilder().setCode(detailedCode)) |
| 1174 | .build(); |
| 1175 | } |
| 1176 | |
Chi Wang | 0f812eb | 2021-07-06 01:14:43 -0700 | [diff] [blame^] | 1177 | /** |
| 1178 | * An {@link OutputStream} that reports all the write operations with {@link |
| 1179 | * DownloadProgressReporter}. |
| 1180 | */ |
| 1181 | private static class ReportingOutputStream extends OutputStream { |
| 1182 | |
| 1183 | private final OutputStream out; |
| 1184 | private final DownloadProgressReporter reporter; |
| 1185 | |
| 1186 | ReportingOutputStream(OutputStream out, DownloadProgressReporter reporter) { |
| 1187 | this.out = out; |
| 1188 | this.reporter = reporter; |
| 1189 | } |
| 1190 | |
| 1191 | @Override |
| 1192 | public void write(byte[] b) throws IOException { |
| 1193 | out.write(b); |
| 1194 | reporter.downloadedBytes(b.length); |
| 1195 | } |
| 1196 | |
| 1197 | @Override |
| 1198 | public void write(byte[] b, int off, int len) throws IOException { |
| 1199 | out.write(b, off, len); |
| 1200 | reporter.downloadedBytes(len); |
| 1201 | } |
| 1202 | |
| 1203 | @Override |
| 1204 | public void write(int b) throws IOException { |
| 1205 | out.write(b); |
| 1206 | reporter.downloadedBytes(1); |
| 1207 | } |
| 1208 | |
| 1209 | @Override |
| 1210 | public void flush() throws IOException { |
| 1211 | out.flush(); |
| 1212 | } |
| 1213 | |
| 1214 | @Override |
| 1215 | public void close() throws IOException { |
| 1216 | out.close(); |
| 1217 | } |
| 1218 | } |
| 1219 | |
Jakob Buchgraber | 584ae24 | 2019-03-29 08:58:41 -0700 | [diff] [blame] | 1220 | /** In-memory representation of action result metadata. */ |
| 1221 | static class ActionResultMetadata { |
| 1222 | |
| 1223 | static class SymlinkMetadata { |
| 1224 | private final Path path; |
| 1225 | private final PathFragment target; |
| 1226 | |
| 1227 | private SymlinkMetadata(Path path, PathFragment target) { |
| 1228 | this.path = path; |
| 1229 | this.target = target; |
| 1230 | } |
| 1231 | |
| 1232 | public Path path() { |
| 1233 | return path; |
| 1234 | } |
| 1235 | |
| 1236 | public PathFragment target() { |
| 1237 | return target; |
| 1238 | } |
| 1239 | } |
| 1240 | |
| 1241 | static class FileMetadata { |
| 1242 | private final Path path; |
| 1243 | private final Digest digest; |
| 1244 | private final boolean isExecutable; |
| 1245 | |
| 1246 | private FileMetadata(Path path, Digest digest, boolean isExecutable) { |
| 1247 | this.path = path; |
| 1248 | this.digest = digest; |
| 1249 | this.isExecutable = isExecutable; |
| 1250 | } |
| 1251 | |
| 1252 | public Path path() { |
| 1253 | return path; |
| 1254 | } |
| 1255 | |
| 1256 | public Digest digest() { |
| 1257 | return digest; |
| 1258 | } |
| 1259 | |
| 1260 | public boolean isExecutable() { |
| 1261 | return isExecutable; |
| 1262 | } |
| 1263 | } |
| 1264 | |
| 1265 | static class DirectoryMetadata { |
| 1266 | private final ImmutableList<FileMetadata> files; |
| 1267 | private final ImmutableList<SymlinkMetadata> symlinks; |
| 1268 | |
| 1269 | private DirectoryMetadata( |
| 1270 | ImmutableList<FileMetadata> files, ImmutableList<SymlinkMetadata> symlinks) { |
| 1271 | this.files = files; |
| 1272 | this.symlinks = symlinks; |
| 1273 | } |
| 1274 | |
| 1275 | public ImmutableList<FileMetadata> files() { |
| 1276 | return files; |
| 1277 | } |
| 1278 | |
| 1279 | public ImmutableList<SymlinkMetadata> symlinks() { |
| 1280 | return symlinks; |
| 1281 | } |
| 1282 | } |
| 1283 | |
| 1284 | private final ImmutableMap<Path, FileMetadata> files; |
| 1285 | private final ImmutableMap<Path, SymlinkMetadata> symlinks; |
| 1286 | private final ImmutableMap<Path, DirectoryMetadata> directories; |
| 1287 | |
| 1288 | private ActionResultMetadata( |
| 1289 | ImmutableMap<Path, FileMetadata> files, |
| 1290 | ImmutableMap<Path, SymlinkMetadata> symlinks, |
| 1291 | ImmutableMap<Path, DirectoryMetadata> directories) { |
| 1292 | this.files = files; |
| 1293 | this.symlinks = symlinks; |
| 1294 | this.directories = directories; |
| 1295 | } |
| 1296 | |
| 1297 | @Nullable |
| 1298 | public FileMetadata file(Path path) { |
| 1299 | return files.get(path); |
| 1300 | } |
| 1301 | |
| 1302 | @Nullable |
| 1303 | public DirectoryMetadata directory(Path path) { |
| 1304 | return directories.get(path); |
| 1305 | } |
| 1306 | |
| 1307 | public Collection<FileMetadata> files() { |
| 1308 | return files.values(); |
| 1309 | } |
| 1310 | |
| 1311 | public ImmutableSet<Entry<Path, DirectoryMetadata>> directories() { |
| 1312 | return directories.entrySet(); |
| 1313 | } |
| 1314 | |
| 1315 | public Collection<SymlinkMetadata> symlinks() { |
| 1316 | return symlinks.values(); |
| 1317 | } |
| 1318 | } |
Hadrien Chauvin | 3d0a04d | 2017-12-20 08:45:45 -0800 | [diff] [blame] | 1319 | } |