buchgr | 357cb1e | 2019-04-03 06:15:02 -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. |
Googler | 4785a95 | 2019-10-22 04:54:34 -0700 | [diff] [blame] | 14 | // |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 15 | |
| 16 | package com.google.devtools.build.lib.remote; |
| 17 | |
Chi Wang | 0eeac8b | 2022-10-18 03:25:31 -0700 | [diff] [blame] | 18 | import static com.google.common.base.Preconditions.checkArgument; |
ajurkowski | 8883c61 | 2021-03-08 08:12:37 -0800 | [diff] [blame] | 19 | import static com.google.common.base.Preconditions.checkNotNull; |
Tiago Quelhas | 32b0f5a | 2022-10-20 07:05:34 -0700 | [diff] [blame] | 20 | import static com.google.common.base.Preconditions.checkState; |
Chi Wang | f4deafb | 2022-10-14 02:50:31 -0700 | [diff] [blame] | 21 | import static com.google.common.collect.ImmutableMap.toImmutableMap; |
| 22 | import static com.google.common.collect.Streams.stream; |
ajurkowski | 8883c61 | 2021-03-08 08:12:37 -0800 | [diff] [blame] | 23 | |
Chi Wang | 0eeac8b | 2022-10-18 03:25:31 -0700 | [diff] [blame] | 24 | import com.google.common.annotations.VisibleForTesting; |
Googler | 97dea59 | 2022-11-08 05:03:04 -0800 | [diff] [blame] | 25 | import com.google.common.collect.ImmutableList; |
Chi Wang | f4deafb | 2022-10-14 02:50:31 -0700 | [diff] [blame] | 26 | import com.google.common.collect.ImmutableMap; |
Chi Wang | 963640a | 2023-02-20 03:20:39 -0800 | [diff] [blame] | 27 | import com.google.devtools.build.lib.actions.ActionInput; |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 28 | import com.google.devtools.build.lib.actions.ActionInputMap; |
Chi Wang | 79b5b5d | 2022-08-22 02:34:05 -0700 | [diff] [blame] | 29 | import com.google.devtools.build.lib.actions.Artifact; |
| 30 | import com.google.devtools.build.lib.actions.Artifact.SpecialArtifact; |
| 31 | import com.google.devtools.build.lib.actions.Artifact.TreeFileArtifact; |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 32 | import com.google.devtools.build.lib.actions.FileArtifactValue; |
| 33 | import com.google.devtools.build.lib.actions.FileArtifactValue.RemoteFileArtifactValue; |
Googler | 2a8ab5b | 2023-03-10 08:20:47 -0800 | [diff] [blame] | 34 | import com.google.devtools.build.lib.actions.MetadataProvider; |
Chi Wang | 29ee191 | 2022-11-29 06:42:31 -0800 | [diff] [blame] | 35 | import com.google.devtools.build.lib.actions.RemoteFileStatus; |
Chi Wang | 79b5b5d | 2022-08-22 02:34:05 -0700 | [diff] [blame] | 36 | import com.google.devtools.build.lib.actions.cache.MetadataInjector; |
Chi Wang | 2876569 | 2022-10-17 04:16:35 -0700 | [diff] [blame] | 37 | import com.google.devtools.build.lib.clock.Clock; |
Chi Wang | 79b5b5d | 2022-08-22 02:34:05 -0700 | [diff] [blame] | 38 | import com.google.devtools.build.lib.skyframe.TreeArtifactValue; |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 39 | import com.google.devtools.build.lib.vfs.DelegateFileSystem; |
Chi Wang | 2876569 | 2022-10-17 04:16:35 -0700 | [diff] [blame] | 40 | import com.google.devtools.build.lib.vfs.DigestHashFunction; |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 41 | import com.google.devtools.build.lib.vfs.Dirent; |
| 42 | import com.google.devtools.build.lib.vfs.FileStatus; |
| 43 | import com.google.devtools.build.lib.vfs.FileSystem; |
| 44 | import com.google.devtools.build.lib.vfs.Path; |
| 45 | import com.google.devtools.build.lib.vfs.PathFragment; |
Tiago Quelhas | 32b0f5a | 2022-10-20 07:05:34 -0700 | [diff] [blame] | 46 | import com.google.devtools.build.lib.vfs.Symlinks; |
Chi Wang | 2876569 | 2022-10-17 04:16:35 -0700 | [diff] [blame] | 47 | import com.google.devtools.build.lib.vfs.inmemoryfs.FileInfo; |
| 48 | import com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryContentInfo; |
| 49 | import com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem; |
Chi Wang | 0eeac8b | 2022-10-18 03:25:31 -0700 | [diff] [blame] | 50 | import com.google.errorprone.annotations.CanIgnoreReturnValue; |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 51 | import java.io.FileNotFoundException; |
| 52 | import java.io.IOException; |
| 53 | import java.io.InputStream; |
Chi Wang | 2876569 | 2022-10-17 04:16:35 -0700 | [diff] [blame] | 54 | import java.io.OutputStream; |
Googler | 4785a95 | 2019-10-22 04:54:34 -0700 | [diff] [blame] | 55 | import java.nio.channels.ReadableByteChannel; |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 56 | import java.util.Collection; |
Googler | 97dea59 | 2022-11-08 05:03:04 -0800 | [diff] [blame] | 57 | import java.util.HashMap; |
| 58 | import java.util.HashSet; |
Chi Wang | 79b5b5d | 2022-08-22 02:34:05 -0700 | [diff] [blame] | 59 | import java.util.Map; |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 60 | import javax.annotation.Nullable; |
| 61 | |
| 62 | /** |
| 63 | * This is a basic implementation and incomplete implementation of an action file system that's been |
Tiago Quelhas | 32b0f5a | 2022-10-20 07:05:34 -0700 | [diff] [blame] | 64 | * tuned to what internal (non-spawn) actions in Bazel currently use. |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 65 | * |
| 66 | * <p>The implementation mostly delegates to the local file system except for the case where an |
ajurkowski | 8883c61 | 2021-03-08 08:12:37 -0800 | [diff] [blame] | 67 | * action input is a remotely stored action output. Most notably {@link |
| 68 | * #getInputStream(PathFragment)} and {@link #createSymbolicLink(PathFragment, PathFragment)}. |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 69 | * |
| 70 | * <p>This implementation only supports creating local action outputs. |
| 71 | */ |
Googler | 2a8ab5b | 2023-03-10 08:20:47 -0800 | [diff] [blame] | 72 | public class RemoteActionFileSystem extends DelegateFileSystem implements MetadataProvider { |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 73 | |
ajurkowski | 8883c61 | 2021-03-08 08:12:37 -0800 | [diff] [blame] | 74 | private final PathFragment execRoot; |
| 75 | private final PathFragment outputBase; |
Googler | 2a8ab5b | 2023-03-10 08:20:47 -0800 | [diff] [blame] | 76 | private final MetadataProvider fileCache; |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 77 | private final ActionInputMap inputArtifactData; |
Chi Wang | f4deafb | 2022-10-14 02:50:31 -0700 | [diff] [blame] | 78 | private final ImmutableMap<PathFragment, Artifact> outputMapping; |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 79 | private final RemoteActionInputFetcher inputFetcher; |
Chi Wang | 2876569 | 2022-10-17 04:16:35 -0700 | [diff] [blame] | 80 | private final RemoteInMemoryFileSystem remoteOutputTree; |
Chi Wang | 79b5b5d | 2022-08-22 02:34:05 -0700 | [diff] [blame] | 81 | |
| 82 | @Nullable private MetadataInjector metadataInjector = null; |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 83 | |
Jakob Buchgraber | 484ffae | 2019-06-19 06:04:12 -0700 | [diff] [blame] | 84 | RemoteActionFileSystem( |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 85 | FileSystem localDelegate, |
| 86 | PathFragment execRootFragment, |
| 87 | String relativeOutputPath, |
| 88 | ActionInputMap inputArtifactData, |
Chi Wang | f4deafb | 2022-10-14 02:50:31 -0700 | [diff] [blame] | 89 | Iterable<Artifact> outputArtifacts, |
Googler | 2a8ab5b | 2023-03-10 08:20:47 -0800 | [diff] [blame] | 90 | MetadataProvider fileCache, |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 91 | RemoteActionInputFetcher inputFetcher) { |
| 92 | super(localDelegate); |
ajurkowski | 8883c61 | 2021-03-08 08:12:37 -0800 | [diff] [blame] | 93 | this.execRoot = checkNotNull(execRootFragment, "execRootFragment"); |
| 94 | this.outputBase = execRoot.getRelative(checkNotNull(relativeOutputPath, "relativeOutputPath")); |
| 95 | this.inputArtifactData = checkNotNull(inputArtifactData, "inputArtifactData"); |
Chi Wang | f4deafb | 2022-10-14 02:50:31 -0700 | [diff] [blame] | 96 | this.outputMapping = |
| 97 | stream(outputArtifacts).collect(toImmutableMap(Artifact::getExecPath, a -> a)); |
Googler | 2a8ab5b | 2023-03-10 08:20:47 -0800 | [diff] [blame] | 98 | this.fileCache = checkNotNull(fileCache, "fileCache"); |
ajurkowski | 8883c61 | 2021-03-08 08:12:37 -0800 | [diff] [blame] | 99 | this.inputFetcher = checkNotNull(inputFetcher, "inputFetcher"); |
Chi Wang | 2876569 | 2022-10-17 04:16:35 -0700 | [diff] [blame] | 100 | this.remoteOutputTree = new RemoteInMemoryFileSystem(getDigestFunction()); |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Chi Wang | 0eeac8b | 2022-10-18 03:25:31 -0700 | [diff] [blame] | 103 | @VisibleForTesting |
| 104 | protected RemoteInMemoryFileSystem getRemoteOutputTree() { |
| 105 | return remoteOutputTree; |
| 106 | } |
| 107 | |
| 108 | @VisibleForTesting |
| 109 | protected FileSystem getLocalFileSystem() { |
| 110 | return delegateFs; |
| 111 | } |
| 112 | |
Jakob Buchgraber | 484ffae | 2019-06-19 06:04:12 -0700 | [diff] [blame] | 113 | /** Returns true if {@code path} is a file that's stored remotely. */ |
| 114 | boolean isRemote(Path path) { |
Googler | 2a8ab5b | 2023-03-10 08:20:47 -0800 | [diff] [blame] | 115 | return isRemote(path.asFragment()); |
| 116 | } |
| 117 | |
| 118 | private boolean isRemote(PathFragment path) { |
| 119 | return getRemoteMetadata(path) != null; |
Jakob Buchgraber | 484ffae | 2019-06-19 06:04:12 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Chi Wang | 79b5b5d | 2022-08-22 02:34:05 -0700 | [diff] [blame] | 122 | public void updateContext(MetadataInjector metadataInjector) { |
| 123 | this.metadataInjector = metadataInjector; |
| 124 | } |
| 125 | |
Chi Wang | 1ebb04b | 2023-03-03 07:13:57 -0800 | [diff] [blame] | 126 | void injectRemoteFile(PathFragment path, byte[] digest, long size, long expireAtEpochMilli) |
| 127 | throws IOException { |
Chi Wang | 0eeac8b | 2022-10-18 03:25:31 -0700 | [diff] [blame] | 128 | if (!isOutput(path)) { |
Chi Wang | 2876569 | 2022-10-17 04:16:35 -0700 | [diff] [blame] | 129 | return; |
Chi Wang | 79b5b5d | 2022-08-22 02:34:05 -0700 | [diff] [blame] | 130 | } |
Chi Wang | 1ebb04b | 2023-03-03 07:13:57 -0800 | [diff] [blame] | 131 | remoteOutputTree.injectRemoteFile(path, digest, size, expireAtEpochMilli); |
Chi Wang | 79b5b5d | 2022-08-22 02:34:05 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Chi Wang | 2876569 | 2022-10-17 04:16:35 -0700 | [diff] [blame] | 134 | void flush() throws IOException { |
Chi Wang | f4deafb | 2022-10-14 02:50:31 -0700 | [diff] [blame] | 135 | checkNotNull(metadataInjector, "metadataInjector is null"); |
| 136 | |
| 137 | for (Map.Entry<PathFragment, Artifact> entry : outputMapping.entrySet()) { |
Tiago Quelhas | 32b0f5a | 2022-10-20 07:05:34 -0700 | [diff] [blame] | 138 | PathFragment path = execRoot.getRelative(entry.getKey()); |
Chi Wang | f4deafb | 2022-10-14 02:50:31 -0700 | [diff] [blame] | 139 | Artifact output = entry.getValue(); |
Tiago Quelhas | 32b0f5a | 2022-10-20 07:05:34 -0700 | [diff] [blame] | 140 | |
Chi Wang | 29ee191 | 2022-11-29 06:42:31 -0800 | [diff] [blame] | 141 | maybeInjectMetadataForSymlink(path, output); |
Chi Wang | 79b5b5d | 2022-08-22 02:34:05 -0700 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | |
Tiago Quelhas | 32b0f5a | 2022-10-20 07:05:34 -0700 | [diff] [blame] | 145 | /** |
| 146 | * Inject metadata for non-symlink outputs that were materialized as a symlink to a remote |
| 147 | * artifact. |
| 148 | * |
| 149 | * <p>If a non-symlink output is materialized as a symlink, the symlink has "copy" semantics, |
| 150 | * i.e., the output metadata is identical to that of the symlink target. For these artifacts, we |
| 151 | * inject their metadata instead of collecting it from the filesystem. This is done for two |
| 152 | * reasons: |
| 153 | * |
| 154 | * <ul> |
| 155 | * <li>It avoids implementing filesystem operations for resolving symlinks and (in the case of a |
| 156 | * tree artifact) listing directories, which are especially tricky since the symlink and its |
| 157 | * target may reside on different filesystems; |
| 158 | * <li>It lets us add a special field to the output metadata to tell the input prefetcher that |
| 159 | * the output should be materialized as a symlink to the original location, which avoids |
| 160 | * fetching multiple copies when multiple symlinks to the same artifact are created in the |
| 161 | * same build. |
Tiago Quelhas | 32b0f5a | 2022-10-20 07:05:34 -0700 | [diff] [blame] | 162 | */ |
Chi Wang | 29ee191 | 2022-11-29 06:42:31 -0800 | [diff] [blame] | 163 | private void maybeInjectMetadataForSymlink(PathFragment linkPath, Artifact output) |
Tiago Quelhas | 32b0f5a | 2022-10-20 07:05:34 -0700 | [diff] [blame] | 164 | throws IOException { |
| 165 | if (output.isSymlink()) { |
Chi Wang | 29ee191 | 2022-11-29 06:42:31 -0800 | [diff] [blame] | 166 | return; |
Tiago Quelhas | 32b0f5a | 2022-10-20 07:05:34 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | Path outputTreePath = remoteOutputTree.getPath(linkPath); |
| 170 | |
| 171 | if (!outputTreePath.exists(Symlinks.NOFOLLOW)) { |
Chi Wang | 29ee191 | 2022-11-29 06:42:31 -0800 | [diff] [blame] | 172 | return; |
Tiago Quelhas | 32b0f5a | 2022-10-20 07:05:34 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | PathFragment targetPath; |
| 176 | try { |
| 177 | targetPath = outputTreePath.readSymbolicLink(); |
| 178 | } catch (NotASymlinkException e) { |
Chi Wang | 29ee191 | 2022-11-29 06:42:31 -0800 | [diff] [blame] | 179 | return; |
Tiago Quelhas | 32b0f5a | 2022-10-20 07:05:34 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | checkState( |
| 183 | targetPath.isAbsolute(), |
| 184 | "non-symlink artifact materialized as symlink must point to absolute path"); |
| 185 | |
| 186 | if (output.isTreeArtifact()) { |
| 187 | TreeArtifactValue metadata = getRemoteTreeMetadata(targetPath); |
| 188 | if (metadata == null) { |
Chi Wang | 29ee191 | 2022-11-29 06:42:31 -0800 | [diff] [blame] | 189 | return; |
Tiago Quelhas | 32b0f5a | 2022-10-20 07:05:34 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | SpecialArtifact parent = (SpecialArtifact) output; |
| 193 | TreeArtifactValue.Builder injectedTree = TreeArtifactValue.newBuilder(parent); |
| 194 | // Avoid a double indirection when the target is already materialized as a symlink. |
| 195 | injectedTree.setMaterializationExecPath( |
| 196 | metadata.getMaterializationExecPath().orElse(targetPath.relativeTo(execRoot))); |
| 197 | // TODO: Check directory content on the local fs to support mixed tree. |
| 198 | for (Map.Entry<TreeFileArtifact, FileArtifactValue> entry : |
| 199 | metadata.getChildValues().entrySet()) { |
| 200 | TreeFileArtifact child = |
| 201 | TreeFileArtifact.createTreeOutput(parent, entry.getKey().getParentRelativePath()); |
| 202 | RemoteFileArtifactValue childMetadata = (RemoteFileArtifactValue) entry.getValue(); |
| 203 | injectedTree.putChild(child, childMetadata); |
| 204 | } |
| 205 | |
| 206 | metadataInjector.injectTree(parent, injectedTree.build()); |
| 207 | } else { |
| 208 | RemoteFileArtifactValue metadata = getRemoteMetadata(targetPath); |
| 209 | if (metadata == null) { |
Chi Wang | 29ee191 | 2022-11-29 06:42:31 -0800 | [diff] [blame] | 210 | return; |
Tiago Quelhas | 32b0f5a | 2022-10-20 07:05:34 -0700 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | RemoteFileArtifactValue injectedMetadata = |
| 214 | RemoteFileArtifactValue.create( |
| 215 | metadata.getDigest(), |
| 216 | metadata.getSize(), |
| 217 | metadata.getLocationIndex(), |
Chi Wang | 1ebb04b | 2023-03-03 07:13:57 -0800 | [diff] [blame] | 218 | metadata.getExpireAtEpochMilli(), |
Tiago Quelhas | 32b0f5a | 2022-10-20 07:05:34 -0700 | [diff] [blame] | 219 | // Avoid a double indirection when the target is already materialized as a symlink. |
| 220 | metadata.getMaterializationExecPath().orElse(targetPath.relativeTo(execRoot))); |
| 221 | |
| 222 | metadataInjector.injectFile(output, injectedMetadata); |
| 223 | } |
Tiago Quelhas | 32b0f5a | 2022-10-20 07:05:34 -0700 | [diff] [blame] | 224 | } |
| 225 | |
Chi Wang | 2876569 | 2022-10-17 04:16:35 -0700 | [diff] [blame] | 226 | private RemoteFileArtifactValue createRemoteMetadata(RemoteFileInfo remoteFile) { |
| 227 | return RemoteFileArtifactValue.create( |
Chi Wang | 1ebb04b | 2023-03-03 07:13:57 -0800 | [diff] [blame] | 228 | remoteFile.getFastDigest(), |
| 229 | remoteFile.getSize(), |
| 230 | /* locationIndex= */ 1, |
| 231 | remoteFile.getExpireAtEpochMilli()); |
Chi Wang | 2876569 | 2022-10-17 04:16:35 -0700 | [diff] [blame] | 232 | } |
| 233 | |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 234 | @Override |
ajurkowski | 8883c61 | 2021-03-08 08:12:37 -0800 | [diff] [blame] | 235 | public String getFileSystemType(PathFragment path) { |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 236 | return "remoteActionFS"; |
| 237 | } |
| 238 | |
| 239 | @Override |
ajurkowski | ea26e0a | 2021-03-22 16:04:20 -0700 | [diff] [blame] | 240 | protected boolean delete(PathFragment path) throws IOException { |
Googler | 9b8304c | 2022-10-19 08:42:24 -0700 | [diff] [blame] | 241 | boolean deleted = super.delete(path); |
Googler | 486a964 | 2023-03-08 01:31:02 -0800 | [diff] [blame] | 242 | if (isOutput(path)) { |
Googler | 9b8304c | 2022-10-19 08:42:24 -0700 | [diff] [blame] | 243 | deleted = remoteOutputTree.getPath(path).delete() || deleted; |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 244 | } |
Googler | 9b8304c | 2022-10-19 08:42:24 -0700 | [diff] [blame] | 245 | return deleted; |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | @Override |
ajurkowski | 8883c61 | 2021-03-08 08:12:37 -0800 | [diff] [blame] | 249 | protected InputStream getInputStream(PathFragment path) throws IOException { |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 250 | downloadFileIfRemote(path); |
Googler | 2a8ab5b | 2023-03-10 08:20:47 -0800 | [diff] [blame] | 251 | // TODO(tjgq): Consider only falling back to the local filesystem for source (non-output) files. |
| 252 | // See getMetadata() for why this isn't currently possible. |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 253 | return super.getInputStream(path); |
| 254 | } |
| 255 | |
| 256 | @Override |
ajurkowski | 8883c61 | 2021-03-08 08:12:37 -0800 | [diff] [blame] | 257 | protected ReadableByteChannel createReadableByteChannel(PathFragment path) throws IOException { |
Googler | 4785a95 | 2019-10-22 04:54:34 -0700 | [diff] [blame] | 258 | downloadFileIfRemote(path); |
ichern | 03be73e | 2019-10-22 10:16:44 -0700 | [diff] [blame] | 259 | return super.createReadableByteChannel(path); |
Googler | 4785a95 | 2019-10-22 04:54:34 -0700 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | @Override |
ajurkowski | 8883c61 | 2021-03-08 08:12:37 -0800 | [diff] [blame] | 263 | public void setLastModifiedTime(PathFragment path, long newTime) throws IOException { |
Chi Wang | 33b514b | 2022-12-12 06:36:08 -0800 | [diff] [blame] | 264 | FileNotFoundException remoteException = null; |
| 265 | try { |
| 266 | // We can't set mtime for a remote file, set mtime of in-memory file node instead. |
| 267 | remoteOutputTree.setLastModifiedTime(path, newTime); |
| 268 | } catch (FileNotFoundException e) { |
| 269 | remoteException = e; |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 270 | } |
Chi Wang | 33b514b | 2022-12-12 06:36:08 -0800 | [diff] [blame] | 271 | |
| 272 | FileNotFoundException localException = null; |
| 273 | try { |
| 274 | super.setLastModifiedTime(path, newTime); |
| 275 | } catch (FileNotFoundException e) { |
| 276 | localException = e; |
| 277 | } |
| 278 | |
| 279 | if (remoteException == null || localException == null) { |
| 280 | return; |
| 281 | } |
| 282 | |
| 283 | localException.addSuppressed(remoteException); |
| 284 | throw localException; |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | @Override |
ajurkowski | 8883c61 | 2021-03-08 08:12:37 -0800 | [diff] [blame] | 288 | protected byte[] getFastDigest(PathFragment path) throws IOException { |
Tiago Quelhas | 32b0f5a | 2022-10-20 07:05:34 -0700 | [diff] [blame] | 289 | RemoteFileArtifactValue m = getRemoteMetadata(path); |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 290 | if (m != null) { |
| 291 | return m.getDigest(); |
| 292 | } |
| 293 | return super.getFastDigest(path); |
| 294 | } |
| 295 | |
| 296 | @Override |
ajurkowski | 8883c61 | 2021-03-08 08:12:37 -0800 | [diff] [blame] | 297 | protected byte[] getDigest(PathFragment path) throws IOException { |
Tiago Quelhas | 32b0f5a | 2022-10-20 07:05:34 -0700 | [diff] [blame] | 298 | RemoteFileArtifactValue m = getRemoteMetadata(path); |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 299 | if (m != null) { |
| 300 | return m.getDigest(); |
| 301 | } |
| 302 | return super.getDigest(path); |
| 303 | } |
| 304 | |
| 305 | // -------------------- File Permissions -------------------- |
Googler | cc712ee | 2022-11-25 03:25:58 -0800 | [diff] [blame] | 306 | // Remote files are always readable, writable and executable since we can't control their |
| 307 | // permissions. |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 308 | |
Googler | 4312c5f | 2022-11-28 01:54:32 -0800 | [diff] [blame] | 309 | private boolean existsInMemory(PathFragment path) { |
| 310 | return remoteOutputTree.getPath(path).isDirectory() || getRemoteMetadata(path) != null; |
| 311 | } |
| 312 | |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 313 | @Override |
ajurkowski | 8883c61 | 2021-03-08 08:12:37 -0800 | [diff] [blame] | 314 | protected boolean isReadable(PathFragment path) throws IOException { |
Googler | 4312c5f | 2022-11-28 01:54:32 -0800 | [diff] [blame] | 315 | return existsInMemory(path) || super.isReadable(path); |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | @Override |
ajurkowski | 8883c61 | 2021-03-08 08:12:37 -0800 | [diff] [blame] | 319 | protected boolean isWritable(PathFragment path) throws IOException { |
Googler | 4312c5f | 2022-11-28 01:54:32 -0800 | [diff] [blame] | 320 | if (existsInMemory(path)) { |
Googler | cc712ee | 2022-11-25 03:25:58 -0800 | [diff] [blame] | 321 | // If path exists locally, also check whether it's writable. We need this check for the case |
| 322 | // where the action need to delete their local outputs but the parent directory is not |
| 323 | // writable. |
| 324 | try { |
| 325 | return super.isWritable(path); |
| 326 | } catch (FileNotFoundException e) { |
| 327 | // Intentionally ignored |
| 328 | return true; |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | return super.isWritable(path); |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | @Override |
ajurkowski | 8883c61 | 2021-03-08 08:12:37 -0800 | [diff] [blame] | 336 | protected boolean isExecutable(PathFragment path) throws IOException { |
Googler | 4312c5f | 2022-11-28 01:54:32 -0800 | [diff] [blame] | 337 | return existsInMemory(path) || super.isExecutable(path); |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | @Override |
ajurkowski | 8883c61 | 2021-03-08 08:12:37 -0800 | [diff] [blame] | 341 | protected void setReadable(PathFragment path, boolean readable) throws IOException { |
Googler | cc712ee | 2022-11-25 03:25:58 -0800 | [diff] [blame] | 342 | try { |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 343 | super.setReadable(path, readable); |
Googler | cc712ee | 2022-11-25 03:25:58 -0800 | [diff] [blame] | 344 | } catch (FileNotFoundException e) { |
Googler | 4312c5f | 2022-11-28 01:54:32 -0800 | [diff] [blame] | 345 | // in case of missing in-memory path, re-throw the error. |
| 346 | if (!existsInMemory(path)) { |
Googler | cc712ee | 2022-11-25 03:25:58 -0800 | [diff] [blame] | 347 | throw e; |
| 348 | } |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 349 | } |
| 350 | } |
| 351 | |
| 352 | @Override |
ajurkowski | 8883c61 | 2021-03-08 08:12:37 -0800 | [diff] [blame] | 353 | public void setWritable(PathFragment path, boolean writable) throws IOException { |
Googler | cc712ee | 2022-11-25 03:25:58 -0800 | [diff] [blame] | 354 | try { |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 355 | super.setWritable(path, writable); |
Googler | cc712ee | 2022-11-25 03:25:58 -0800 | [diff] [blame] | 356 | } catch (FileNotFoundException e) { |
Googler | 4312c5f | 2022-11-28 01:54:32 -0800 | [diff] [blame] | 357 | // in case of missing in-memory path, re-throw the error. |
| 358 | if (!existsInMemory(path)) { |
Googler | cc712ee | 2022-11-25 03:25:58 -0800 | [diff] [blame] | 359 | throw e; |
| 360 | } |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 361 | } |
| 362 | } |
| 363 | |
| 364 | @Override |
ajurkowski | 8883c61 | 2021-03-08 08:12:37 -0800 | [diff] [blame] | 365 | protected void setExecutable(PathFragment path, boolean executable) throws IOException { |
Googler | cc712ee | 2022-11-25 03:25:58 -0800 | [diff] [blame] | 366 | try { |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 367 | super.setExecutable(path, executable); |
Googler | cc712ee | 2022-11-25 03:25:58 -0800 | [diff] [blame] | 368 | } catch (FileNotFoundException e) { |
Googler | 4312c5f | 2022-11-28 01:54:32 -0800 | [diff] [blame] | 369 | // in case of missing in-memory path, re-throw the error. |
| 370 | if (!existsInMemory(path)) { |
Googler | cc712ee | 2022-11-25 03:25:58 -0800 | [diff] [blame] | 371 | throw e; |
| 372 | } |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 373 | } |
| 374 | } |
| 375 | |
| 376 | @Override |
ajurkowski | 8883c61 | 2021-03-08 08:12:37 -0800 | [diff] [blame] | 377 | protected void chmod(PathFragment path, int mode) throws IOException { |
Googler | cc712ee | 2022-11-25 03:25:58 -0800 | [diff] [blame] | 378 | try { |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 379 | super.chmod(path, mode); |
Googler | cc712ee | 2022-11-25 03:25:58 -0800 | [diff] [blame] | 380 | } catch (FileNotFoundException e) { |
Googler | 4312c5f | 2022-11-28 01:54:32 -0800 | [diff] [blame] | 381 | // in case of missing in-memory path, re-throw the error. |
| 382 | if (!existsInMemory(path)) { |
Googler | cc712ee | 2022-11-25 03:25:58 -0800 | [diff] [blame] | 383 | throw e; |
| 384 | } |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 385 | } |
| 386 | } |
| 387 | |
| 388 | // -------------------- Symlinks -------------------- |
| 389 | |
| 390 | @Override |
ajurkowski | 8883c61 | 2021-03-08 08:12:37 -0800 | [diff] [blame] | 391 | protected PathFragment readSymbolicLink(PathFragment path) throws IOException { |
Tiago Quelhas | 32b0f5a | 2022-10-20 07:05:34 -0700 | [diff] [blame] | 392 | RemoteFileArtifactValue m = getRemoteMetadata(path); |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 393 | if (m != null) { |
| 394 | // We don't support symlinks as remote action outputs. |
| 395 | throw new IOException(path + " is not a symbolic link"); |
| 396 | } |
| 397 | return super.readSymbolicLink(path); |
| 398 | } |
| 399 | |
| 400 | @Override |
ajurkowski | 8883c61 | 2021-03-08 08:12:37 -0800 | [diff] [blame] | 401 | protected void createSymbolicLink(PathFragment linkPath, PathFragment targetFragment) |
| 402 | throws IOException { |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 403 | super.createSymbolicLink(linkPath, targetFragment); |
Googler | 486a964 | 2023-03-08 01:31:02 -0800 | [diff] [blame] | 404 | if (isOutput(linkPath)) { |
Tiago Quelhas | 32b0f5a | 2022-10-20 07:05:34 -0700 | [diff] [blame] | 405 | remoteOutputTree.getPath(linkPath).createSymbolicLink(targetFragment); |
| 406 | } |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | // -------------------- Implementations based on stat() -------------------- |
| 410 | |
| 411 | @Override |
ajurkowski | 8883c61 | 2021-03-08 08:12:37 -0800 | [diff] [blame] | 412 | protected long getLastModifiedTime(PathFragment path, boolean followSymlinks) throws IOException { |
Chi Wang | 33b514b | 2022-12-12 06:36:08 -0800 | [diff] [blame] | 413 | try { |
| 414 | // We can't get mtime for a remote file, use mtime of in-memory file node instead. |
| 415 | return remoteOutputTree |
| 416 | .getPath(path) |
| 417 | .getLastModifiedTime(followSymlinks ? Symlinks.FOLLOW : Symlinks.NOFOLLOW); |
| 418 | } catch (FileNotFoundException e) { |
| 419 | // Intentionally ignored |
| 420 | } |
| 421 | |
| 422 | return super.getLastModifiedTime(path, followSymlinks); |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | @Override |
ajurkowski | 8883c61 | 2021-03-08 08:12:37 -0800 | [diff] [blame] | 426 | protected long getFileSize(PathFragment path, boolean followSymlinks) throws IOException { |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 427 | FileStatus stat = stat(path, followSymlinks); |
| 428 | return stat.getSize(); |
| 429 | } |
| 430 | |
| 431 | @Override |
ajurkowski | 8883c61 | 2021-03-08 08:12:37 -0800 | [diff] [blame] | 432 | protected boolean isFile(PathFragment path, boolean followSymlinks) { |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 433 | FileStatus stat = statNullable(path, followSymlinks); |
| 434 | return stat != null && stat.isFile(); |
| 435 | } |
| 436 | |
| 437 | @Override |
ajurkowski | 8883c61 | 2021-03-08 08:12:37 -0800 | [diff] [blame] | 438 | protected boolean isSymbolicLink(PathFragment path) { |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 439 | FileStatus stat = statNullable(path, /* followSymlinks= */ false); |
| 440 | return stat != null && stat.isSymbolicLink(); |
| 441 | } |
| 442 | |
| 443 | @Override |
ajurkowski | 8883c61 | 2021-03-08 08:12:37 -0800 | [diff] [blame] | 444 | protected boolean isDirectory(PathFragment path, boolean followSymlinks) { |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 445 | FileStatus stat = statNullable(path, followSymlinks); |
| 446 | return stat != null && stat.isDirectory(); |
| 447 | } |
| 448 | |
| 449 | @Override |
ajurkowski | 8883c61 | 2021-03-08 08:12:37 -0800 | [diff] [blame] | 450 | protected boolean isSpecialFile(PathFragment path, boolean followSymlinks) { |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 451 | FileStatus stat = statNullable(path, followSymlinks); |
| 452 | return stat != null && stat.isDirectory(); |
| 453 | } |
| 454 | |
| 455 | @Override |
ajurkowski | 8883c61 | 2021-03-08 08:12:37 -0800 | [diff] [blame] | 456 | protected boolean exists(PathFragment path, boolean followSymlinks) { |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 457 | try { |
| 458 | return statIfFound(path, followSymlinks) != null; |
| 459 | } catch (IOException e) { |
| 460 | return false; |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | @Override |
ajurkowski | 8883c61 | 2021-03-08 08:12:37 -0800 | [diff] [blame] | 465 | public boolean exists(PathFragment path) { |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 466 | return exists(path, /* followSymlinks= */ true); |
| 467 | } |
| 468 | |
| 469 | @Nullable |
| 470 | @Override |
ajurkowski | 8883c61 | 2021-03-08 08:12:37 -0800 | [diff] [blame] | 471 | protected FileStatus statIfFound(PathFragment path, boolean followSymlinks) throws IOException { |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 472 | try { |
| 473 | return stat(path, followSymlinks); |
| 474 | } catch (FileNotFoundException e) { |
| 475 | return null; |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | @Nullable |
| 480 | @Override |
ajurkowski | 8883c61 | 2021-03-08 08:12:37 -0800 | [diff] [blame] | 481 | protected FileStatus statNullable(PathFragment path, boolean followSymlinks) { |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 482 | try { |
| 483 | return stat(path, followSymlinks); |
| 484 | } catch (IOException e) { |
| 485 | return null; |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | @Override |
ajurkowski | 8883c61 | 2021-03-08 08:12:37 -0800 | [diff] [blame] | 490 | protected FileStatus stat(PathFragment path, boolean followSymlinks) throws IOException { |
Tiago Quelhas | 32b0f5a | 2022-10-20 07:05:34 -0700 | [diff] [blame] | 491 | RemoteFileArtifactValue m = getRemoteMetadata(path); |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 492 | if (m != null) { |
| 493 | return statFromRemoteMetadata(m); |
| 494 | } |
| 495 | return super.stat(path, followSymlinks); |
| 496 | } |
| 497 | |
janakr | 0fdf1a1 | 2020-10-08 13:04:12 -0700 | [diff] [blame] | 498 | private static FileStatus statFromRemoteMetadata(RemoteFileArtifactValue m) { |
Chi Wang | 29ee191 | 2022-11-29 06:42:31 -0800 | [diff] [blame] | 499 | return new RemoteFileStatus() { |
| 500 | @Override |
| 501 | public byte[] getDigest() { |
| 502 | return m.getDigest(); |
| 503 | } |
| 504 | |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 505 | @Override |
| 506 | public boolean isFile() { |
| 507 | return m.getType().isFile(); |
| 508 | } |
| 509 | |
| 510 | @Override |
| 511 | public boolean isDirectory() { |
| 512 | return m.getType().isDirectory(); |
| 513 | } |
| 514 | |
| 515 | @Override |
| 516 | public boolean isSymbolicLink() { |
| 517 | return m.getType().isSymlink(); |
| 518 | } |
| 519 | |
| 520 | @Override |
| 521 | public boolean isSpecialFile() { |
| 522 | return m.getType().isSpecialFile(); |
| 523 | } |
| 524 | |
| 525 | @Override |
| 526 | public long getSize() { |
| 527 | return m.getSize(); |
| 528 | } |
| 529 | |
| 530 | @Override |
| 531 | public long getLastModifiedTime() { |
| 532 | return m.getModifiedTime(); |
| 533 | } |
| 534 | |
| 535 | @Override |
| 536 | public long getLastChangeTime() { |
| 537 | return m.getModifiedTime(); |
| 538 | } |
| 539 | |
| 540 | @Override |
| 541 | public long getNodeId() { |
janakr | 0fdf1a1 | 2020-10-08 13:04:12 -0700 | [diff] [blame] | 542 | throw new UnsupportedOperationException("Cannot get node id for " + m); |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 543 | } |
Chi Wang | 29ee191 | 2022-11-29 06:42:31 -0800 | [diff] [blame] | 544 | |
| 545 | @Override |
| 546 | public RemoteFileArtifactValue getRemoteMetadata() { |
| 547 | return m; |
| 548 | } |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 549 | }; |
| 550 | } |
| 551 | |
Googler | 2a8ab5b | 2023-03-10 08:20:47 -0800 | [diff] [blame] | 552 | @Override |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 553 | @Nullable |
Googler | 2a8ab5b | 2023-03-10 08:20:47 -0800 | [diff] [blame] | 554 | public ActionInput getInput(String execPath) { |
| 555 | ActionInput input = inputArtifactData.getInput(execPath); |
| 556 | if (input != null) { |
| 557 | return input; |
| 558 | } |
| 559 | input = outputMapping.get(PathFragment.create(execPath)); |
| 560 | if (input != null) { |
| 561 | return input; |
| 562 | } |
| 563 | if (!isOutput(execRoot.getRelative(execPath))) { |
| 564 | return fileCache.getInput(execPath); |
| 565 | } |
| 566 | return null; |
| 567 | } |
| 568 | |
| 569 | ActionInput getActionInput(PathFragment path) { |
| 570 | return getInput(path.relativeTo(execRoot).getPathString()); |
Chi Wang | 963640a | 2023-02-20 03:20:39 -0800 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | @Nullable |
Googler | 2a8ab5b | 2023-03-10 08:20:47 -0800 | [diff] [blame] | 574 | @Override |
| 575 | public FileArtifactValue getMetadata(ActionInput input) throws IOException { |
| 576 | PathFragment execPath = input.getExecPath(); |
| 577 | FileArtifactValue m = getMetadataByExecPath(execPath); |
| 578 | if (m != null) { |
| 579 | return m; |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 580 | } |
Googler | 2a8ab5b | 2023-03-10 08:20:47 -0800 | [diff] [blame] | 581 | // TODO(tjgq): Consider only falling back to the local filesystem for source (non-output) files. |
| 582 | // The output fallback is needed when an undeclared output of a spawn is consumed by another |
| 583 | // spawn within the same action; specifically, when the first spawn is local but the second is |
| 584 | // remote, or, in the context of a failed test attempt, when both spawns are remote but the |
| 585 | // first one fails. In both cases, we don't currently inject the output metadata for the first |
| 586 | // spawn; if we did so, then we could stop falling back here. |
| 587 | return fileCache.getMetadata(input); |
| 588 | } |
| 589 | |
| 590 | @Nullable |
| 591 | private FileArtifactValue getMetadataByExecPath(PathFragment execPath) { |
ajurkowski | aad74a3 | 2021-07-12 12:56:47 -0700 | [diff] [blame] | 592 | FileArtifactValue m = inputArtifactData.getMetadata(execPath); |
Googler | 2a8ab5b | 2023-03-10 08:20:47 -0800 | [diff] [blame] | 593 | if (m != null) { |
| 594 | return m; |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 595 | } |
Chi Wang | 79b5b5d | 2022-08-22 02:34:05 -0700 | [diff] [blame] | 596 | |
Chi Wang | 2876569 | 2022-10-17 04:16:35 -0700 | [diff] [blame] | 597 | RemoteFileInfo remoteFile = |
Googler | 2a8ab5b | 2023-03-10 08:20:47 -0800 | [diff] [blame] | 598 | remoteOutputTree.getRemoteFileInfo( |
| 599 | execRoot.getRelative(execPath), /* followSymlinks= */ true); |
Chi Wang | 2876569 | 2022-10-17 04:16:35 -0700 | [diff] [blame] | 600 | if (remoteFile != null) { |
| 601 | return createRemoteMetadata(remoteFile); |
| 602 | } |
| 603 | |
| 604 | return null; |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 605 | } |
| 606 | |
Tiago Quelhas | 32b0f5a | 2022-10-20 07:05:34 -0700 | [diff] [blame] | 607 | @Nullable |
Googler | 2a8ab5b | 2023-03-10 08:20:47 -0800 | [diff] [blame] | 608 | RemoteFileArtifactValue getRemoteMetadata(PathFragment path) { |
| 609 | if (!isOutput(path)) { |
| 610 | return null; |
| 611 | } |
| 612 | FileArtifactValue m = getMetadataByExecPath(path.relativeTo(execRoot)); |
| 613 | if (m != null && m.isRemote()) { |
| 614 | return (RemoteFileArtifactValue) m; |
| 615 | } |
| 616 | return null; |
| 617 | } |
| 618 | |
| 619 | @Nullable |
Tiago Quelhas | 32b0f5a | 2022-10-20 07:05:34 -0700 | [diff] [blame] | 620 | private TreeArtifactValue getRemoteTreeMetadata(PathFragment path) { |
Googler | 486a964 | 2023-03-08 01:31:02 -0800 | [diff] [blame] | 621 | if (!isOutput(path)) { |
Tiago Quelhas | 32b0f5a | 2022-10-20 07:05:34 -0700 | [diff] [blame] | 622 | return null; |
| 623 | } |
Googler | 2a8ab5b | 2023-03-10 08:20:47 -0800 | [diff] [blame] | 624 | TreeArtifactValue m = inputArtifactData.getTreeMetadata(path.relativeTo(execRoot)); |
Tiago Quelhas | 32b0f5a | 2022-10-20 07:05:34 -0700 | [diff] [blame] | 625 | // TODO: Handle partially remote tree artifacts. |
| 626 | if (m != null && m.isEntirelyRemote()) { |
| 627 | return m; |
| 628 | } |
| 629 | // TODO(tjgq): Synthesize TreeArtifactValue from remoteOutputTree. |
| 630 | return null; |
| 631 | } |
| 632 | |
ajurkowski | 8883c61 | 2021-03-08 08:12:37 -0800 | [diff] [blame] | 633 | private void downloadFileIfRemote(PathFragment path) throws IOException { |
Tiago Quelhas | 32b0f5a | 2022-10-20 07:05:34 -0700 | [diff] [blame] | 634 | FileArtifactValue m = getRemoteMetadata(path); |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 635 | if (m != null) { |
| 636 | try { |
Chi Wang | 963640a | 2023-02-20 03:20:39 -0800 | [diff] [blame] | 637 | inputFetcher.downloadFile(delegateFs.getPath(path), getActionInput(path), m); |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 638 | } catch (InterruptedException e) { |
| 639 | Thread.currentThread().interrupt(); |
| 640 | throw new IOException( |
| 641 | String.format("Received interrupt while fetching file '%s'", path), e); |
| 642 | } |
| 643 | } |
| 644 | } |
| 645 | |
Chi Wang | 0eeac8b | 2022-10-18 03:25:31 -0700 | [diff] [blame] | 646 | private boolean isOutput(PathFragment path) { |
| 647 | return path.startsWith(outputBase); |
| 648 | } |
| 649 | |
| 650 | @Override |
| 651 | public void renameTo(PathFragment sourcePath, PathFragment targetPath) throws IOException { |
| 652 | checkArgument(isOutput(sourcePath), "sourcePath must be an output path"); |
| 653 | checkArgument(isOutput(targetPath), "targetPath must be an output path"); |
| 654 | |
| 655 | FileNotFoundException remoteException = null; |
| 656 | try { |
| 657 | remoteOutputTree.renameTo(sourcePath, targetPath); |
| 658 | } catch (FileNotFoundException e) { |
| 659 | remoteException = e; |
| 660 | } |
| 661 | |
| 662 | FileNotFoundException localException = null; |
| 663 | try { |
| 664 | delegateFs.renameTo(sourcePath, targetPath); |
| 665 | } catch (FileNotFoundException e) { |
| 666 | localException = e; |
| 667 | } |
| 668 | |
| 669 | if (remoteException == null || localException == null) { |
| 670 | return; |
| 671 | } |
| 672 | |
| 673 | localException.addSuppressed(remoteException); |
| 674 | throw localException; |
| 675 | } |
| 676 | |
| 677 | @Override |
| 678 | public void createDirectoryAndParents(PathFragment path) throws IOException { |
| 679 | super.createDirectoryAndParents(path); |
Googler | 486a964 | 2023-03-08 01:31:02 -0800 | [diff] [blame] | 680 | if (isOutput(path)) { |
Chi Wang | 0eeac8b | 2022-10-18 03:25:31 -0700 | [diff] [blame] | 681 | remoteOutputTree.createDirectoryAndParents(path); |
| 682 | } |
| 683 | } |
| 684 | |
| 685 | @CanIgnoreReturnValue |
| 686 | @Override |
| 687 | public boolean createDirectory(PathFragment path) throws IOException { |
| 688 | boolean created = delegateFs.createDirectory(path); |
Googler | 486a964 | 2023-03-08 01:31:02 -0800 | [diff] [blame] | 689 | if (isOutput(path)) { |
Chi Wang | 0eeac8b | 2022-10-18 03:25:31 -0700 | [diff] [blame] | 690 | created = remoteOutputTree.createDirectory(path) || created; |
| 691 | } |
| 692 | return created; |
| 693 | } |
| 694 | |
Googler | 97dea59 | 2022-11-08 05:03:04 -0800 | [diff] [blame] | 695 | @Override |
| 696 | protected ImmutableList<String> getDirectoryEntries(PathFragment path) throws IOException { |
| 697 | HashSet<String> entries = new HashSet<>(); |
| 698 | |
| 699 | boolean ignoredNotFoundInRemote = false; |
| 700 | if (isOutput(path)) { |
| 701 | try { |
| 702 | delegateFs.getPath(path).getDirectoryEntries().stream() |
| 703 | .map(Path::getBaseName) |
| 704 | .forEach(entries::add); |
| 705 | ignoredNotFoundInRemote = true; |
| 706 | } catch (FileNotFoundException ignored) { |
| 707 | // Intentionally ignored |
| 708 | } |
| 709 | } |
| 710 | |
| 711 | try { |
| 712 | remoteOutputTree.getPath(path).getDirectoryEntries().stream() |
| 713 | .map(Path::getBaseName) |
| 714 | .forEach(entries::add); |
| 715 | } catch (FileNotFoundException e) { |
| 716 | if (!ignoredNotFoundInRemote) { |
| 717 | throw e; |
| 718 | } |
| 719 | } |
| 720 | |
| 721 | // sort entries to get a deterministic order. |
| 722 | return ImmutableList.sortedCopyOf(entries); |
| 723 | } |
| 724 | |
| 725 | @Override |
| 726 | protected Collection<Dirent> readdir(PathFragment path, boolean followSymlinks) |
| 727 | throws IOException { |
| 728 | HashMap<String, Dirent> entries = new HashMap<>(); |
| 729 | |
| 730 | boolean ignoredNotFoundInRemote = false; |
| 731 | if (isOutput(path)) { |
| 732 | try { |
| 733 | for (var entry : |
| 734 | delegateFs |
| 735 | .getPath(path) |
| 736 | .readdir(followSymlinks ? Symlinks.FOLLOW : Symlinks.NOFOLLOW)) { |
| 737 | entries.put(entry.getName(), entry); |
| 738 | } |
| 739 | ignoredNotFoundInRemote = true; |
| 740 | } catch (FileNotFoundException ignored) { |
| 741 | // Intentionally ignored |
| 742 | } |
| 743 | } |
| 744 | |
| 745 | try { |
| 746 | for (var entry : |
| 747 | remoteOutputTree |
| 748 | .getPath(path) |
| 749 | .readdir(followSymlinks ? Symlinks.FOLLOW : Symlinks.NOFOLLOW)) { |
| 750 | entries.put(entry.getName(), entry); |
| 751 | } |
| 752 | } catch (FileNotFoundException e) { |
| 753 | if (!ignoredNotFoundInRemote) { |
| 754 | throw e; |
| 755 | } |
| 756 | } |
| 757 | |
| 758 | // sort entries to get a deterministic order. |
| 759 | return ImmutableList.sortedCopyOf(entries.values()); |
| 760 | } |
| 761 | |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 762 | /* |
| 763 | * -------------------- TODO(buchgr): Not yet implemented -------------------- |
| 764 | * |
| 765 | * The below methods have not (yet) been properly implemented due to time constraints mostly and |
Tiago Quelhas | 32b0f5a | 2022-10-20 07:05:34 -0700 | [diff] [blame] | 766 | * with little risk as they currently don't seem to be used by internal actions in Bazel. However, |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 767 | * before making the --experimental_remote_download_outputs flag non-experimental we should make |
| 768 | * sure to fully implement this file system. |
| 769 | */ |
| 770 | |
| 771 | @Override |
ajurkowski | 8883c61 | 2021-03-08 08:12:37 -0800 | [diff] [blame] | 772 | protected void createFSDependentHardLink(PathFragment linkPath, PathFragment originalPath) |
| 773 | throws IOException { |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 774 | super.createFSDependentHardLink(linkPath, originalPath); |
| 775 | } |
| 776 | |
| 777 | @Override |
ajurkowski | 8883c61 | 2021-03-08 08:12:37 -0800 | [diff] [blame] | 778 | protected void createHardLink(PathFragment linkPath, PathFragment originalPath) |
| 779 | throws IOException { |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 780 | super.createHardLink(linkPath, originalPath); |
| 781 | } |
Chi Wang | 2876569 | 2022-10-17 04:16:35 -0700 | [diff] [blame] | 782 | |
| 783 | static class RemoteInMemoryFileSystem extends InMemoryFileSystem { |
| 784 | |
| 785 | public RemoteInMemoryFileSystem(DigestHashFunction hashFunction) { |
| 786 | super(hashFunction); |
| 787 | } |
| 788 | |
| 789 | @Override |
| 790 | protected synchronized OutputStream getOutputStream(PathFragment path, boolean append) |
| 791 | throws IOException { |
| 792 | // To get an output stream from remote file, we need to first stage it. |
| 793 | throw new IllegalStateException("Shouldn't be called directly"); |
| 794 | } |
| 795 | |
| 796 | @Override |
| 797 | protected FileInfo newFile(Clock clock, PathFragment path) { |
| 798 | return new RemoteFileInfo(clock); |
| 799 | } |
| 800 | |
Chi Wang | 1ebb04b | 2023-03-03 07:13:57 -0800 | [diff] [blame] | 801 | void injectRemoteFile(PathFragment path, byte[] digest, long size, long expireAtEpochMilli) |
| 802 | throws IOException { |
Chi Wang | 2876569 | 2022-10-17 04:16:35 -0700 | [diff] [blame] | 803 | createDirectoryAndParents(path.getParentDirectory()); |
| 804 | InMemoryContentInfo node = getOrCreateWritableInode(path); |
| 805 | // If a node was already existed and is not a remote file node (i.e. directory or symlink node |
| 806 | // ), throw an error. |
| 807 | if (!(node instanceof RemoteFileInfo)) { |
| 808 | throw new IOException("Could not inject into " + node); |
| 809 | } |
| 810 | |
| 811 | RemoteFileInfo remoteFileInfo = (RemoteFileInfo) node; |
Chi Wang | 1ebb04b | 2023-03-03 07:13:57 -0800 | [diff] [blame] | 812 | remoteFileInfo.set(digest, size, expireAtEpochMilli); |
Chi Wang | 2876569 | 2022-10-17 04:16:35 -0700 | [diff] [blame] | 813 | } |
| 814 | |
| 815 | @Nullable |
| 816 | RemoteFileInfo getRemoteFileInfo(PathFragment path, boolean followSymlinks) { |
| 817 | InMemoryContentInfo node = inodeStatErrno(path, followSymlinks).inode(); |
| 818 | if (!(node instanceof RemoteFileInfo)) { |
| 819 | return null; |
| 820 | } |
| 821 | return (RemoteFileInfo) node; |
| 822 | } |
| 823 | } |
| 824 | |
| 825 | static class RemoteFileInfo extends FileInfo { |
| 826 | |
| 827 | private byte[] digest; |
| 828 | private long size; |
Chi Wang | 2876569 | 2022-10-17 04:16:35 -0700 | [diff] [blame] | 829 | |
Chi Wang | 1ebb04b | 2023-03-03 07:13:57 -0800 | [diff] [blame] | 830 | private long expireAtEpochMilli; |
| 831 | |
Chi Wang | 2876569 | 2022-10-17 04:16:35 -0700 | [diff] [blame] | 832 | RemoteFileInfo(Clock clock) { |
| 833 | super(clock); |
| 834 | } |
| 835 | |
Chi Wang | 1ebb04b | 2023-03-03 07:13:57 -0800 | [diff] [blame] | 836 | private void set(byte[] digest, long size, long expireAtEpochMilli) { |
Chi Wang | 2876569 | 2022-10-17 04:16:35 -0700 | [diff] [blame] | 837 | this.digest = digest; |
| 838 | this.size = size; |
Chi Wang | 1ebb04b | 2023-03-03 07:13:57 -0800 | [diff] [blame] | 839 | this.expireAtEpochMilli = expireAtEpochMilli; |
Chi Wang | 2876569 | 2022-10-17 04:16:35 -0700 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | @Override |
| 843 | public OutputStream getOutputStream(boolean append) throws IOException { |
| 844 | throw new IllegalStateException("Shouldn't be called directly"); |
| 845 | } |
| 846 | |
| 847 | @Override |
| 848 | public InputStream getInputStream() throws IOException { |
| 849 | throw new IllegalStateException("Shouldn't be called directly"); |
| 850 | } |
| 851 | |
| 852 | @Override |
| 853 | public byte[] getxattr(String name) throws IOException { |
| 854 | throw new IllegalStateException("Shouldn't be called directly"); |
| 855 | } |
| 856 | |
| 857 | @Override |
| 858 | public byte[] getFastDigest() { |
| 859 | return digest; |
| 860 | } |
| 861 | |
| 862 | @Override |
| 863 | public long getSize() { |
| 864 | return size; |
| 865 | } |
Chi Wang | 1ebb04b | 2023-03-03 07:13:57 -0800 | [diff] [blame] | 866 | |
| 867 | public long getExpireAtEpochMilli() { |
| 868 | return expireAtEpochMilli; |
| 869 | } |
Chi Wang | 2876569 | 2022-10-17 04:16:35 -0700 | [diff] [blame] | 870 | } |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 871 | } |