Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2014 The Bazel Authors. All rights reserved. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 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.skyframe; |
| 15 | |
shahan | 602cc85 | 2018-06-06 20:09:57 -0700 | [diff] [blame] | 16 | import com.google.devtools.build.lib.actions.FileStateValue; |
janakr | b8bc284 | 2021-12-08 12:48:02 -0800 | [diff] [blame] | 17 | import com.google.devtools.build.lib.io.InconsistentFilesystemException; |
Googler | ba4862d | 2019-05-03 06:13:23 -0700 | [diff] [blame] | 18 | import com.google.devtools.build.lib.skyframe.ExternalFilesHelper.FileType; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 19 | import com.google.devtools.build.lib.util.io.TimestampGranularityMonitor; |
| 20 | import com.google.devtools.build.lib.vfs.RootedPath; |
janakr | fc1d79c | 2022-01-27 13:02:07 -0800 | [diff] [blame] | 21 | import com.google.devtools.build.lib.vfs.SyscallCache; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 22 | import com.google.devtools.build.skyframe.SkyFunction; |
| 23 | import com.google.devtools.build.skyframe.SkyFunctionException; |
| 24 | import com.google.devtools.build.skyframe.SkyKey; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 25 | import java.io.IOException; |
janakr | fc1d79c | 2022-01-27 13:02:07 -0800 | [diff] [blame] | 26 | import java.util.function.Supplier; |
Googler | 5cc598c | 2022-07-06 03:29:45 -0700 | [diff] [blame] | 27 | import javax.annotation.Nullable; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 28 | |
| 29 | /** |
| 30 | * A {@link SkyFunction} for {@link FileStateValue}s. |
| 31 | * |
| 32 | * <p>Merely calls FileStateValue#create, but also has special handling for files outside the |
| 33 | * package roots (see {@link ExternalFilesHelper}). |
| 34 | */ |
| 35 | public class FileStateFunction implements SkyFunction { |
| 36 | |
janakr | fc1d79c | 2022-01-27 13:02:07 -0800 | [diff] [blame] | 37 | private final Supplier<TimestampGranularityMonitor> tsgm; |
janakr | b2a9434 | 2022-02-05 22:02:14 -0800 | [diff] [blame] | 38 | private final SyscallCache syscallCache; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 39 | private final ExternalFilesHelper externalFilesHelper; |
| 40 | |
djasper | 7faa0ef | 2019-03-28 10:00:00 -0700 | [diff] [blame] | 41 | public FileStateFunction( |
janakr | fc1d79c | 2022-01-27 13:02:07 -0800 | [diff] [blame] | 42 | Supplier<TimestampGranularityMonitor> tsgm, |
janakr | b2a9434 | 2022-02-05 22:02:14 -0800 | [diff] [blame] | 43 | SyscallCache syscallCache, |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 44 | ExternalFilesHelper externalFilesHelper) { |
| 45 | this.tsgm = tsgm; |
djasper | 7faa0ef | 2019-03-28 10:00:00 -0700 | [diff] [blame] | 46 | this.syscallCache = syscallCache; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 47 | this.externalFilesHelper = externalFilesHelper; |
| 48 | } |
| 49 | |
janakr | b8bc284 | 2021-12-08 12:48:02 -0800 | [diff] [blame] | 50 | // InconsistentFilesystemException catch block needs to be separate from IOException catch block |
| 51 | // below because Java does "single dispatch": the runtime type of e is all that is considered when |
| 52 | // deciding which overload of FileStateFunctionException() to call. |
| 53 | @SuppressWarnings("UseMultiCatch") |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 54 | @Override |
Googler | 5cc598c | 2022-07-06 03:29:45 -0700 | [diff] [blame] | 55 | @Nullable |
Googler | da3c009 | 2018-10-11 14:48:17 -0700 | [diff] [blame] | 56 | public FileStateValue compute(SkyKey skyKey, Environment env) |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 57 | throws FileStateFunctionException, InterruptedException { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 58 | RootedPath rootedPath = (RootedPath) skyKey.argument(); |
Michajlo Matijkiw | db11094 | 2015-03-31 23:41:02 +0000 | [diff] [blame] | 59 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 60 | try { |
wyv | 9732d23 | 2021-05-20 02:57:24 -0700 | [diff] [blame] | 61 | FileType fileType = externalFilesHelper.maybeHandleExternalFile(rootedPath, env); |
Michajlo Matijkiw | db11094 | 2015-03-31 23:41:02 +0000 | [diff] [blame] | 62 | if (env.valuesMissing()) { |
| 63 | return null; |
| 64 | } |
Googler | cbf8159 | 2022-06-21 05:00:30 -0700 | [diff] [blame] | 65 | if (fileType == FileType.EXTERNAL_REPO) { |
Googler | ba4862d | 2019-05-03 06:13:23 -0700 | [diff] [blame] | 66 | // do not use syscallCache as files under repositories get generated during the build |
janakr | d1b2f96 | 2022-03-04 13:33:44 -0800 | [diff] [blame] | 67 | return FileStateValue.create(rootedPath, SyscallCache.NO_CACHE, tsgm.get()); |
Googler | ba4862d | 2019-05-03 06:13:23 -0700 | [diff] [blame] | 68 | } |
janakr | b2a9434 | 2022-02-05 22:02:14 -0800 | [diff] [blame] | 69 | return FileStateValue.create(rootedPath, syscallCache, tsgm.get()); |
Nathan Harmata | d4f7594 | 2016-10-18 08:55:17 +0000 | [diff] [blame] | 70 | } catch (ExternalFilesHelper.NonexistentImmutableExternalFileException e) { |
| 71 | return FileStateValue.NONEXISTENT_FILE_STATE_NODE; |
janakr | b8bc284 | 2021-12-08 12:48:02 -0800 | [diff] [blame] | 72 | } catch (InconsistentFilesystemException e) { |
| 73 | throw new FileStateFunctionException(e); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 74 | } catch (IOException e) { |
| 75 | throw new FileStateFunctionException(e); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 79 | /** |
janakr | c64db33 | 2021-11-04 09:15:18 -0700 | [diff] [blame] | 80 | * Used to declare all the exception types that can be wrapped in the exception thrown by {@link |
| 81 | * FileStateFunction#compute}. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 82 | */ |
janakr | c64db33 | 2021-11-04 09:15:18 -0700 | [diff] [blame] | 83 | public static final class FileStateFunctionException extends SkyFunctionException { |
janakr | b8bc284 | 2021-12-08 12:48:02 -0800 | [diff] [blame] | 84 | private final boolean isCatastrophic; |
| 85 | |
| 86 | private FileStateFunctionException(InconsistentFilesystemException e) { |
| 87 | super(e, Transience.TRANSIENT); |
| 88 | this.isCatastrophic = true; |
| 89 | } |
| 90 | |
janakr | c64db33 | 2021-11-04 09:15:18 -0700 | [diff] [blame] | 91 | private FileStateFunctionException(IOException e) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 92 | super(e, Transience.TRANSIENT); |
janakr | b8bc284 | 2021-12-08 12:48:02 -0800 | [diff] [blame] | 93 | this.isCatastrophic = false; |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public boolean isCatastrophic() { |
| 98 | return isCatastrophic; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 99 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 100 | } |
| 101 | } |