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. |
| 14 | |
| 15 | package com.google.devtools.build.lib.remote; |
| 16 | |
| 17 | import com.google.common.base.Preconditions; |
| 18 | import com.google.common.collect.ImmutableList; |
| 19 | import com.google.devtools.build.lib.actions.Action; |
| 20 | import com.google.devtools.build.lib.actions.ActionInputMap; |
| 21 | import com.google.devtools.build.lib.actions.Artifact; |
Jakob Buchgraber | 484ffae | 2019-06-19 06:04:12 -0700 | [diff] [blame] | 22 | import com.google.devtools.build.lib.actions.ArtifactPathResolver; |
felly | f6408d6 | 2019-07-16 12:51:14 -0700 | [diff] [blame] | 23 | import com.google.devtools.build.lib.actions.FilesetOutputSymlink; |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 24 | import com.google.devtools.build.lib.actions.cache.MetadataHandler; |
| 25 | import com.google.devtools.build.lib.events.EventHandler; |
| 26 | import com.google.devtools.build.lib.vfs.BatchStat; |
| 27 | import com.google.devtools.build.lib.vfs.FileSystem; |
| 28 | import com.google.devtools.build.lib.vfs.ModifiedFileSet; |
| 29 | import com.google.devtools.build.lib.vfs.OutputService; |
| 30 | import com.google.devtools.build.lib.vfs.Path; |
| 31 | import com.google.devtools.build.lib.vfs.PathFragment; |
| 32 | import com.google.devtools.build.lib.vfs.Root; |
Jakob Buchgraber | 484ffae | 2019-06-19 06:04:12 -0700 | [diff] [blame] | 33 | import java.util.Collection; |
| 34 | import java.util.Map; |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 35 | import java.util.UUID; |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 36 | import javax.annotation.Nullable; |
| 37 | |
| 38 | /** Output service implementation for the remote module */ |
| 39 | public class RemoteOutputService implements OutputService { |
| 40 | |
| 41 | @Nullable private RemoteActionInputFetcher actionInputFetcher; |
| 42 | |
| 43 | void setActionInputFetcher(RemoteActionInputFetcher actionInputFetcher) { |
| 44 | this.actionInputFetcher = Preconditions.checkNotNull(actionInputFetcher, "actionInputFetcher"); |
| 45 | } |
| 46 | |
| 47 | @Override |
| 48 | public ActionFileSystemType actionFileSystemType() { |
| 49 | return actionInputFetcher != null |
| 50 | ? ActionFileSystemType.STAGE_REMOTE_FILES |
| 51 | : ActionFileSystemType.DISABLED; |
| 52 | } |
| 53 | |
| 54 | @Nullable |
| 55 | @Override |
| 56 | public FileSystem createActionFileSystem( |
| 57 | FileSystem sourceDelegate, |
| 58 | PathFragment execRootFragment, |
| 59 | String relativeOutputPath, |
| 60 | ImmutableList<Root> sourceRoots, |
| 61 | ActionInputMap inputArtifactData, |
mschaller | b20f512 | 2020-04-23 21:36:25 -0700 | [diff] [blame] | 62 | Iterable<Artifact> outputArtifacts, |
| 63 | boolean trackFailedRemoteReads) { |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 64 | Preconditions.checkNotNull(actionInputFetcher, "actionInputFetcher"); |
| 65 | return new RemoteActionFileSystem( |
| 66 | sourceDelegate, |
| 67 | execRootFragment, |
| 68 | relativeOutputPath, |
| 69 | inputArtifactData, |
| 70 | actionInputFetcher); |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public String getFilesSystemName() { |
| 75 | return "remoteActionFS"; |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public ModifiedFileSet startBuild( |
| 80 | EventHandler eventHandler, UUID buildId, boolean finalizeActions) { |
| 81 | return ModifiedFileSet.EVERYTHING_MODIFIED; |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public void finalizeBuild(boolean buildSuccessful) { |
| 86 | // Intentionally left empty. |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public void finalizeAction(Action action, MetadataHandler metadataHandler) { |
| 91 | // Intentionally left empty. |
| 92 | } |
| 93 | |
| 94 | @Nullable |
| 95 | @Override |
| 96 | public BatchStat getBatchStatter() { |
| 97 | return null; |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public boolean canCreateSymlinkTree() { |
| 102 | /* TODO(buchgr): Optimize symlink creation for remote execution */ |
| 103 | return false; |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public void createSymlinkTree( |
ulfjack | 1a80bb8 | 2019-11-26 07:52:35 -0800 | [diff] [blame] | 108 | Map<PathFragment, PathFragment> symlinks, PathFragment symlinkTreeRoot) { |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 109 | throw new UnsupportedOperationException(); |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public void clean() { |
| 114 | // Intentionally left empty. |
| 115 | } |
| 116 | |
| 117 | @Override |
Jakob Buchgraber | 484ffae | 2019-06-19 06:04:12 -0700 | [diff] [blame] | 118 | public boolean isRemoteFile(Artifact artifact) { |
| 119 | Path path = artifact.getPath(); |
| 120 | return path.getFileSystem() instanceof RemoteActionFileSystem |
| 121 | && ((RemoteActionFileSystem) path.getFileSystem()).isRemote(path); |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public boolean supportsPathResolverForArtifactValues() { |
| 126 | return actionFileSystemType() != ActionFileSystemType.DISABLED; |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public ArtifactPathResolver createPathResolverForArtifactValues( |
| 131 | PathFragment execRoot, |
| 132 | String relativeOutputPath, |
| 133 | FileSystem fileSystem, |
| 134 | ImmutableList<Root> pathEntries, |
| 135 | ActionInputMap actionInputMap, |
| 136 | Map<Artifact, Collection<Artifact>> expandedArtifacts, |
felly | f6408d6 | 2019-07-16 12:51:14 -0700 | [diff] [blame] | 137 | Map<Artifact, ImmutableList<FilesetOutputSymlink>> filesets) { |
Jakob Buchgraber | 484ffae | 2019-06-19 06:04:12 -0700 | [diff] [blame] | 138 | FileSystem remoteFileSystem = |
| 139 | new RemoteActionFileSystem( |
| 140 | fileSystem, execRoot, relativeOutputPath, actionInputMap, actionInputFetcher); |
| 141 | return ArtifactPathResolver.createPathResolver(remoteFileSystem, fileSystem.getPath(execRoot)); |
buchgr | 357cb1e | 2019-04-03 06:15:02 -0700 | [diff] [blame] | 142 | } |
| 143 | } |