blob: 70dc4728c9bb4e513bb212a6daa796c8571caf81 [file] [log] [blame]
Damien Martin-Guillerezf88f4d82015-09-25 13:56:55 +00001// Copyright 2014 The Bazel Authors. All rights reserved.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01002//
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
shahane35e8cf2018-06-18 08:14:01 -070015package com.google.devtools.build.lib.vfs;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010016
shahane35e8cf2018-06-18 08:14:01 -070017import com.google.common.collect.ImmutableList;
felly943a9c72018-08-09 12:55:47 -070018import com.google.common.collect.ImmutableMap;
Eric Fellheimerf3b43af2015-11-13 19:51:20 +000019import com.google.devtools.build.lib.actions.Action;
shahane35e8cf2018-06-18 08:14:01 -070020import com.google.devtools.build.lib.actions.ActionInputMap;
21import com.google.devtools.build.lib.actions.Artifact;
tomlu880508c2018-08-03 11:21:29 -070022import com.google.devtools.build.lib.actions.ArtifactPathResolver;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010023import com.google.devtools.build.lib.actions.BuildFailedException;
Eric Fellheimerf3b43af2015-11-13 19:51:20 +000024import com.google.devtools.build.lib.actions.EnvironmentalExecException;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010025import com.google.devtools.build.lib.actions.ExecException;
felly943a9c72018-08-09 12:55:47 -070026import com.google.devtools.build.lib.actions.FilesetOutputSymlink;
mschallerf6f452d2019-11-18 09:16:53 -080027import com.google.devtools.build.lib.actions.LostInputsActionExecutionException;
shahane35e8cf2018-06-18 08:14:01 -070028import com.google.devtools.build.lib.actions.MetadataConsumer;
Eric Fellheimerf3b43af2015-11-13 19:51:20 +000029import com.google.devtools.build.lib.actions.cache.MetadataHandler;
ulfjack75483b52017-07-11 13:36:41 +020030import com.google.devtools.build.lib.events.EventHandler;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010031import com.google.devtools.build.lib.util.AbruptExitException;
felly943a9c72018-08-09 12:55:47 -070032import com.google.devtools.build.skyframe.SkyFunction.Environment;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010033import java.io.IOException;
tomlufe1dd882018-08-15 12:17:42 -070034import java.util.Collection;
35import java.util.Map;
Eric Fellheimer2db6a742015-04-28 21:38:43 +000036import java.util.UUID;
shahane35e8cf2018-06-18 08:14:01 -070037import javax.annotation.Nullable;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010038
39/**
40 * An OutputService retains control over the Blaze output tree, and provides a higher level of
41 * abstraction compared to the VFS layer.
42 *
43 * <p>Higher-level facilities include batch statting, cleaning the output tree, creating symlink
44 * trees, and out-of-band insertion of metadata into the tree.
45 */
46public interface OutputService {
47
buchgr7b515522019-03-29 04:42:17 -070048 /** Properties of the action file system implementation provided by this output service. */
49 enum ActionFileSystemType {
50
51 /** Action file system is disabled */
52 DISABLED,
53
54 /**
55 * The action file system implementation does not take over the output base but complements the
56 * file system by being able to stage remote outputs accessed as inputs by local actions, as
57 * used by Bazel.
58 */
59 STAGE_REMOTE_FILES,
60
61 /**
62 * The action file system implementation is fully featured in-memory file system implementation
63 * and takes full control of the output base, as used by Blaze.
64 */
65 IN_MEMORY_FILE_SYSTEM;
66
67 public boolean inMemoryFileSystem() {
68 return this == IN_MEMORY_FILE_SYSTEM;
69 }
70
71 public boolean isEnabled() {
72 return this != DISABLED;
73 }
74 }
75
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010076 /**
77 * @return the name of filesystem, akin to what you might see in /proc/mounts
78 */
79 String getFilesSystemName();
80
81 /**
steinman39c00d22020-03-20 15:23:10 -070082 * Returns true if Bazel should trust (and not verify) build artifacts that were last seen
83 * remotely and do not exist locally.
84 */
85 public default boolean shouldTrustRemoteArtifacts() {
86 return true;
87 }
88
89 /**
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010090 * Start the build.
91 *
Eric Fellheimer2db6a742015-04-28 21:38:43 +000092 * @param buildId the UUID build identifier
steinman39c00d22020-03-20 15:23:10 -070093 * @param finalizeActions whether this build is finalizing actions so that the output service can
94 * track output tree modifications
Eric Fellheimerf3b43af2015-11-13 19:51:20 +000095 * @return a ModifiedFileSet of changed output files.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010096 * @throws BuildFailedException if build preparation failed
97 * @throws InterruptedException
98 */
ulfjack75483b52017-07-11 13:36:41 +020099 ModifiedFileSet startBuild(EventHandler eventHandler, UUID buildId, boolean finalizeActions)
Eric Fellheimer2db6a742015-04-28 21:38:43 +0000100 throws BuildFailedException, AbruptExitException, InterruptedException;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100101
102 /**
103 * Finish the build.
104 *
105 * @param buildSuccessful iff build was successful
106 * @throws BuildFailedException on failure
107 */
Rumou Duan2756c022016-10-06 15:57:36 +0000108 void finalizeBuild(boolean buildSuccessful)
109 throws BuildFailedException, AbruptExitException, InterruptedException;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100110
Eric Fellheimerf3b43af2015-11-13 19:51:20 +0000111 /** Notify the output service of a completed action. */
112 void finalizeAction(Action action, MetadataHandler metadataHandler)
ulfjack85b07732019-09-20 09:04:55 -0700113 throws IOException, EnvironmentalExecException, InterruptedException;
Eric Fellheimerf3b43af2015-11-13 19:51:20 +0000114
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100115 /**
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100116 * @return the BatchStat instance or null.
117 */
118 BatchStat getBatchStatter();
119
120 /**
121 * @return true iff createSymlinkTree() is available.
122 */
123 boolean canCreateSymlinkTree();
124
125 /**
126 * Creates the symlink tree
127 *
ulfjack8f075d22019-11-26 02:48:42 -0800128 * @param symlinks the symlinks to create
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100129 * @param symlinkTreeRoot the symlink tree root, relative to the execRoot
130 * @throws ExecException on failure
131 * @throws InterruptedException
132 */
ulfjack1a80bb82019-11-26 07:52:35 -0800133 void createSymlinkTree(Map<PathFragment, PathFragment> symlinks, PathFragment symlinkTreeRoot)
ulfjack8f075d22019-11-26 02:48:42 -0800134 throws ExecException, InterruptedException;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100135
136 /**
137 * Cleans the entire output tree.
138 *
139 * @throws ExecException on failure
140 * @throws InterruptedException
141 */
142 void clean() throws ExecException, InterruptedException;
143
shahane35e8cf2018-06-18 08:14:01 -0700144 /** @return true iff the file actually lives on a remote server */
145 boolean isRemoteFile(Artifact file);
146
buchgr7b515522019-03-29 04:42:17 -0700147 default ActionFileSystemType actionFileSystemType() {
148 return ActionFileSystemType.DISABLED;
shahane35e8cf2018-06-18 08:14:01 -0700149 }
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100150
151 /**
shahane35e8cf2018-06-18 08:14:01 -0700152 * @param sourceDelegate filesystem for reading source files (excludes output files)
153 * @param execRootFragment absolute path fragment pointing to the execution root
154 * @param relativeOutputPath execution root relative path to output
155 * @param sourceRoots list of directories on the package path (from {@link
156 * com.google.devtools.build.lib.pkgcache.PathPackageLocator})
157 * @param inputArtifactData information about required inputs to the action
shahane35e8cf2018-06-18 08:14:01 -0700158 * @param outputArtifacts required outputs of the action
mschallerb20f5122020-04-23 21:36:25 -0700159 * @param trackFailedRemoteReads whether to track failed remote reads to make LostInput exceptions
buchgr7b515522019-03-29 04:42:17 -0700160 * @return an action-scoped filesystem if {@link #supportsActionFileSystem} is not {@code NONE}
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100161 */
shahane35e8cf2018-06-18 08:14:01 -0700162 @Nullable
163 default FileSystem createActionFileSystem(
164 FileSystem sourceDelegate,
165 PathFragment execRootFragment,
166 String relativeOutputPath,
167 ImmutableList<Root> sourceRoots,
168 ActionInputMap inputArtifactData,
mschallerb20f5122020-04-23 21:36:25 -0700169 Iterable<Artifact> outputArtifacts,
170 boolean trackFailedRemoteReads) {
shahane35e8cf2018-06-18 08:14:01 -0700171 return null;
172 }
173
174 /**
tomlu880508c2018-08-03 11:21:29 -0700175 * Updates the context used by the filesystem returned by {@link #createActionFileSystem}.
shahane35e8cf2018-06-18 08:14:01 -0700176 *
177 * <p>Should be called as context changes throughout action execution.
178 *
tomlu880508c2018-08-03 11:21:29 -0700179 * @param actionFileSystem must be a filesystem returned by {@link #createActionFileSystem}.
felly943a9c72018-08-09 12:55:47 -0700180 * @param filesets The Fileset symlinks known for this action.
shahane35e8cf2018-06-18 08:14:01 -0700181 */
182 default void updateActionFileSystemContext(
tomlu73eccc22018-09-06 08:02:37 -0700183 FileSystem actionFileSystem,
184 Environment env,
185 MetadataConsumer consumer,
186 ImmutableMap<Artifact, ImmutableList<FilesetOutputSymlink>> filesets)
felly943a9c72018-08-09 12:55:47 -0700187 throws IOException {}
tomlu880508c2018-08-03 11:21:29 -0700188
mschallerf6f452d2019-11-18 09:16:53 -0800189 /**
190 * Checks the filesystem returned by {@link #createActionFileSystem} for errors attributable to
191 * lost inputs.
192 */
193 default void checkActionFileSystemForLostInputs(FileSystem actionFileSystem, Action action)
194 throws LostInputsActionExecutionException {}
195
tomlu880508c2018-08-03 11:21:29 -0700196 default boolean supportsPathResolverForArtifactValues() {
197 return false;
198 }
199
200 default ArtifactPathResolver createPathResolverForArtifactValues(
201 PathFragment execRoot,
felly2765ca42018-12-28 13:38:55 -0800202 String relativeOutputPath,
tomlu880508c2018-08-03 11:21:29 -0700203 FileSystem fileSystem,
204 ImmutableList<Root> pathEntries,
tomlufe1dd882018-08-15 12:17:42 -0700205 ActionInputMap actionInputMap,
felly5d5da7a2019-01-11 10:59:20 -0800206 Map<Artifact, Collection<Artifact>> expandedArtifacts,
fellyf6408d62019-07-16 12:51:14 -0700207 Map<Artifact, ImmutableList<FilesetOutputSymlink>> filesets)
208 throws IOException {
tomlu880508c2018-08-03 11:21:29 -0700209 throw new IllegalStateException("Path resolver not supported by this class");
210 }
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100211}