Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 1 | // Copyright 2015 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.skyframe; |
| 15 | |
Googler | fe72dbb | 2021-12-29 07:12:53 -0800 | [diff] [blame] | 16 | import static com.google.common.collect.ImmutableList.toImmutableList; |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 17 | import static com.google.common.truth.Truth.assertThat; |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 18 | import static com.google.common.truth.Truth.assertWithMessage; |
janakr | 933c301 | 2021-07-13 07:44:15 -0700 | [diff] [blame] | 19 | import static java.util.concurrent.TimeUnit.SECONDS; |
ajurkowski | 64795c2 | 2022-04-01 10:38:44 -0700 | [diff] [blame] | 20 | import static org.mockito.ArgumentMatchers.anyLong; |
| 21 | import static org.mockito.Mockito.doNothing; |
| 22 | import static org.mockito.Mockito.mock; |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 23 | |
janakr | fc1d79c | 2022-01-27 13:02:07 -0800 | [diff] [blame] | 24 | import com.google.common.base.Suppliers; |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 25 | import com.google.common.collect.ImmutableList; |
| 26 | import com.google.common.collect.ImmutableMap; |
| 27 | import com.google.common.collect.ImmutableSet; |
buchgr | 4992ae2 | 2019-03-20 04:23:32 -0700 | [diff] [blame] | 28 | import com.google.common.hash.HashCode; |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 29 | import com.google.common.util.concurrent.Runnables; |
| 30 | import com.google.devtools.build.lib.actions.Action; |
janakr | efb3f15 | 2019-06-05 17:42:34 -0700 | [diff] [blame] | 31 | import com.google.devtools.build.lib.actions.ActionLookupData; |
Googler | fe72dbb | 2021-12-29 07:12:53 -0800 | [diff] [blame] | 32 | import com.google.devtools.build.lib.actions.ActionLookupKey; |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 33 | import com.google.devtools.build.lib.actions.Artifact; |
Googler | fe72dbb | 2021-12-29 07:12:53 -0800 | [diff] [blame] | 34 | import com.google.devtools.build.lib.actions.Artifact.ArchivedTreeArtifact; |
Michael Thvedt | e4a7b079 | 2016-02-09 12:15:53 +0000 | [diff] [blame] | 35 | import com.google.devtools.build.lib.actions.Artifact.SpecialArtifact; |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 36 | import com.google.devtools.build.lib.actions.Artifact.TreeFileArtifact; |
tomlu | 1cdcdf9 | 2018-01-16 11:07:51 -0800 | [diff] [blame] | 37 | import com.google.devtools.build.lib.actions.ArtifactRoot; |
Googler | f0b0c39 | 2021-01-27 17:56:52 -0800 | [diff] [blame] | 38 | import com.google.devtools.build.lib.actions.ArtifactRoot.RootType; |
shahan | 602cc85 | 2018-06-06 20:09:57 -0700 | [diff] [blame] | 39 | import com.google.devtools.build.lib.actions.FileArtifactValue; |
buchgr | 4992ae2 | 2019-03-20 04:23:32 -0700 | [diff] [blame] | 40 | import com.google.devtools.build.lib.actions.FileArtifactValue.RemoteFileArtifactValue; |
shahan | 602cc85 | 2018-06-06 20:09:57 -0700 | [diff] [blame] | 41 | import com.google.devtools.build.lib.actions.FileStateValue; |
| 42 | import com.google.devtools.build.lib.actions.FileValue; |
janakr | 13b737a | 2021-07-02 14:24:25 -0700 | [diff] [blame] | 43 | import com.google.devtools.build.lib.actions.ThreadStateReceiver; |
janakr | aea0560 | 2019-05-22 15:41:29 -0700 | [diff] [blame] | 44 | import com.google.devtools.build.lib.actions.util.ActionsTestUtil; |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 45 | import com.google.devtools.build.lib.actions.util.TestAction; |
Kristina Chodorow | f9fdc8d | 2015-12-08 12:49:31 +0000 | [diff] [blame] | 46 | import com.google.devtools.build.lib.analysis.BlazeDirectories; |
janakr | 3b63a4e | 2017-09-14 09:55:40 +0200 | [diff] [blame] | 47 | import com.google.devtools.build.lib.analysis.ServerDirectories; |
lberki | 41cfcc7 | 2019-08-05 02:55:36 -0700 | [diff] [blame] | 48 | import com.google.devtools.build.lib.clock.BlazeClock; |
Googler | fe72dbb | 2021-12-29 07:12:53 -0800 | [diff] [blame] | 49 | import com.google.devtools.build.lib.cmdline.Label; |
ulfjack | 1e1a775 | 2019-12-10 21:17:58 -0800 | [diff] [blame] | 50 | import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder; |
| 51 | import com.google.devtools.build.lib.collect.nestedset.Order; |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 52 | import com.google.devtools.build.lib.events.NullEventHandler; |
janakr | e2af68f | 2021-03-18 15:11:30 -0700 | [diff] [blame] | 53 | import com.google.devtools.build.lib.io.FileSymlinkCycleUniquenessFunction; |
| 54 | import com.google.devtools.build.lib.io.FileSymlinkInfiniteExpansionUniquenessFunction; |
janakr | 15e15c2 | 2019-01-30 11:24:49 -0800 | [diff] [blame] | 55 | import com.google.devtools.build.lib.packages.WorkspaceFileValue; |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 56 | import com.google.devtools.build.lib.pkgcache.PathPackageLocator; |
| 57 | import com.google.devtools.build.lib.skyframe.DirtinessCheckerUtils.BasicFilesystemDirtinessChecker; |
Nathan Harmata | d4f7594 | 2016-10-18 08:55:17 +0000 | [diff] [blame] | 58 | import com.google.devtools.build.lib.skyframe.ExternalFilesHelper.ExternalFileAction; |
ajurkowski | 64795c2 | 2022-04-01 10:38:44 -0700 | [diff] [blame] | 59 | import com.google.devtools.build.lib.skyframe.FilesystemValueChecker.ModifiedOutputsReceiver; |
jhorvitz | 1d675d1 | 2022-04-28 16:23:21 -0700 | [diff] [blame^] | 60 | import com.google.devtools.build.lib.skyframe.PackageFunction.GlobbingStrategy; |
John Cater | 5e9ce94 | 2016-10-12 17:23:30 +0000 | [diff] [blame] | 61 | import com.google.devtools.build.lib.skyframe.PackageLookupFunction.CrossRepositoryLabelViolationStrategy; |
Googler | fe72dbb | 2021-12-29 07:12:53 -0800 | [diff] [blame] | 62 | import com.google.devtools.build.lib.testutil.ManualClock; |
Luis Fernando Pino Duque | be10218 | 2016-05-23 14:03:55 +0000 | [diff] [blame] | 63 | import com.google.devtools.build.lib.testutil.TestConstants; |
michajlo | 8083e32 | 2020-03-20 13:32:52 -0700 | [diff] [blame] | 64 | import com.google.devtools.build.lib.testutil.TestPackageFactoryBuilderFactory; |
Kristina Chodorow | f9fdc8d | 2015-12-08 12:49:31 +0000 | [diff] [blame] | 65 | import com.google.devtools.build.lib.testutil.TestRuleClassProvider; |
janakr | 933c301 | 2021-07-13 07:44:15 -0700 | [diff] [blame] | 66 | import com.google.devtools.build.lib.testutil.TestUtils; |
laszlocsomor | 29bdf63 | 2018-03-13 02:50:46 -0700 | [diff] [blame] | 67 | import com.google.devtools.build.lib.testutil.TimestampGranularityUtils; |
Laszlo Csomor | a278aec | 2018-03-09 04:07:31 -0800 | [diff] [blame] | 68 | import com.google.devtools.build.lib.util.io.OutErr; |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 69 | import com.google.devtools.build.lib.util.io.TimestampGranularityMonitor; |
| 70 | import com.google.devtools.build.lib.vfs.BatchStat; |
buchgr | 4992ae2 | 2019-03-20 04:23:32 -0700 | [diff] [blame] | 71 | import com.google.devtools.build.lib.vfs.DigestHashFunction; |
nharmata | d533996 | 2022-02-11 14:44:08 -0800 | [diff] [blame] | 72 | import com.google.devtools.build.lib.vfs.FileStateKey; |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 73 | import com.google.devtools.build.lib.vfs.FileStatus; |
| 74 | import com.google.devtools.build.lib.vfs.FileStatusWithDigest; |
| 75 | import com.google.devtools.build.lib.vfs.FileStatusWithDigestAdapter; |
Googler | fe72dbb | 2021-12-29 07:12:53 -0800 | [diff] [blame] | 76 | import com.google.devtools.build.lib.vfs.FileSystem; |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 77 | import com.google.devtools.build.lib.vfs.FileSystemUtils; |
Eric Fellheimer | e659072 | 2015-11-17 17:07:48 +0000 | [diff] [blame] | 78 | import com.google.devtools.build.lib.vfs.ModifiedFileSet; |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 79 | import com.google.devtools.build.lib.vfs.Path; |
| 80 | import com.google.devtools.build.lib.vfs.PathFragment; |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 81 | import com.google.devtools.build.lib.vfs.Root; |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 82 | import com.google.devtools.build.lib.vfs.RootedPath; |
| 83 | import com.google.devtools.build.lib.vfs.Symlinks; |
janakr | fc1d79c | 2022-01-27 13:02:07 -0800 | [diff] [blame] | 84 | import com.google.devtools.build.lib.vfs.SyscallCache; |
Googler | fe72dbb | 2021-12-29 07:12:53 -0800 | [diff] [blame] | 85 | import com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem; |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 86 | import com.google.devtools.build.skyframe.Differencer.Diff; |
Googler | 1002867 | 2018-10-25 12:14:34 -0700 | [diff] [blame] | 87 | import com.google.devtools.build.skyframe.EvaluationContext; |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 88 | import com.google.devtools.build.skyframe.EvaluationResult; |
| 89 | import com.google.devtools.build.skyframe.InMemoryMemoizingEvaluator; |
| 90 | import com.google.devtools.build.skyframe.MemoizingEvaluator; |
| 91 | import com.google.devtools.build.skyframe.RecordingDifferencer; |
janakr | 1cde872 | 2017-10-10 03:22:21 +0200 | [diff] [blame] | 92 | import com.google.devtools.build.skyframe.SequencedRecordingDifferencer; |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 93 | import com.google.devtools.build.skyframe.SkyFunction; |
| 94 | import com.google.devtools.build.skyframe.SkyFunctionName; |
| 95 | import com.google.devtools.build.skyframe.SkyKey; |
| 96 | import com.google.devtools.build.skyframe.SkyValue; |
ajurkowski | bdc60b0 | 2021-11-24 08:47:05 -0800 | [diff] [blame] | 97 | import com.google.testing.junit.testparameterinjector.TestParameter; |
| 98 | import com.google.testing.junit.testparameterinjector.TestParameterInjector; |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 99 | import java.io.IOException; |
| 100 | import java.util.ArrayList; |
| 101 | import java.util.Arrays; |
| 102 | import java.util.Collection; |
| 103 | import java.util.HashMap; |
| 104 | import java.util.List; |
| 105 | import java.util.Map; |
| 106 | import java.util.UUID; |
janakr | 933c301 | 2021-07-13 07:44:15 -0700 | [diff] [blame] | 107 | import java.util.concurrent.CountDownLatch; |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 108 | import java.util.concurrent.atomic.AtomicReference; |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 109 | import javax.annotation.Nullable; |
Janak Ramakrishnan | cba1645 | 2016-07-29 02:17:02 +0000 | [diff] [blame] | 110 | import org.junit.Before; |
steinman | 7184cb3 | 2020-04-20 14:33:03 -0700 | [diff] [blame] | 111 | import org.junit.Ignore; |
Janak Ramakrishnan | cba1645 | 2016-07-29 02:17:02 +0000 | [diff] [blame] | 112 | import org.junit.Test; |
| 113 | import org.junit.runner.RunWith; |
ajurkowski | 64795c2 | 2022-04-01 10:38:44 -0700 | [diff] [blame] | 114 | import org.mockito.ArgumentCaptor; |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 115 | |
jhorvitz | 3daedc3 | 2020-07-22 18:33:55 -0700 | [diff] [blame] | 116 | /** Tests for {@link FilesystemValueChecker}. */ |
ajurkowski | bdc60b0 | 2021-11-24 08:47:05 -0800 | [diff] [blame] | 117 | @RunWith(TestParameterInjector.class) |
Googler | fe72dbb | 2021-12-29 07:12:53 -0800 | [diff] [blame] | 118 | public final class FilesystemValueCheckerTest { |
| 119 | private static final int FSVC_THREADS_FOR_TEST = 200; |
| 120 | private static final ActionLookupKey ACTION_LOOKUP_KEY = |
| 121 | new ActionLookupKey() { |
| 122 | @Override |
| 123 | public SkyFunctionName functionName() { |
| 124 | return SkyFunctionName.FOR_TESTING; |
| 125 | } |
| 126 | |
| 127 | @Nullable |
| 128 | @Override |
| 129 | public Label getLabel() { |
| 130 | return null; |
| 131 | } |
| 132 | |
| 133 | @Nullable |
| 134 | @Override |
| 135 | public BuildConfigurationKey getConfigurationKey() { |
| 136 | return null; |
| 137 | } |
| 138 | }; |
| 139 | private static final ActionLookupData ACTION_LOOKUP_DATA = actionLookupData(0); |
Googler | 1002867 | 2018-10-25 12:14:34 -0700 | [diff] [blame] | 140 | private static final EvaluationContext EVALUATION_OPTIONS = |
| 141 | EvaluationContext.newBuilder() |
| 142 | .setKeepGoing(false) |
| 143 | .setNumThreads(SkyframeExecutor.DEFAULT_THREAD_COUNT) |
michajlo | 7a485be | 2020-07-30 11:08:46 -0700 | [diff] [blame] | 144 | .setEventHandler(NullEventHandler.INSTANCE) |
Googler | 1002867 | 2018-10-25 12:14:34 -0700 | [diff] [blame] | 145 | .build(); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 146 | |
Googler | fe72dbb | 2021-12-29 07:12:53 -0800 | [diff] [blame] | 147 | private final MockFileSystem fs = new MockFileSystem(); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 148 | private RecordingDifferencer differencer; |
| 149 | private MemoizingEvaluator evaluator; |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 150 | private Path pkgRoot; |
Googler | fe72dbb | 2021-12-29 07:12:53 -0800 | [diff] [blame] | 151 | @TestParameter private BatchStatMode batchStat; |
| 152 | |
ajurkowski | 64795c2 | 2022-04-01 10:38:44 -0700 | [diff] [blame] | 153 | private final ModifiedOutputsReceiver mockModifiedOutputsReceiver = |
| 154 | mock(ModifiedOutputsReceiver.class); |
| 155 | private final ArgumentCaptor<Artifact> modifiedOutputsCaptor = |
| 156 | ArgumentCaptor.forClass(Artifact.class); |
| 157 | |
Googler | fe72dbb | 2021-12-29 07:12:53 -0800 | [diff] [blame] | 158 | private SpecialArtifact createTreeArtifact(String relPath) throws IOException { |
| 159 | String outSegment = "bin"; |
| 160 | Path outputDir = fs.getPath("/" + outSegment); |
| 161 | Path outputPath = outputDir.getRelative(relPath); |
| 162 | outputDir.createDirectory(); |
| 163 | ArtifactRoot derivedRoot = |
| 164 | ArtifactRoot.asDerivedRoot(fs.getPath("/"), RootType.Output, outSegment); |
| 165 | return ActionsTestUtil.createTreeArtifactWithGeneratingAction( |
| 166 | derivedRoot, |
| 167 | derivedRoot.getExecPath().getRelative(derivedRoot.getRoot().relativize(outputPath))); |
| 168 | } |
| 169 | |
| 170 | private static ActionExecutionValue actionValueWithTreeArtifacts(List<TreeFileArtifact> contents) |
| 171 | throws IOException { |
| 172 | return actionValueWithTreeArtifacts(contents, ImmutableList.of()); |
| 173 | } |
| 174 | |
| 175 | private static ActionExecutionValue actionValueWithTreeArtifacts( |
| 176 | Iterable<TreeFileArtifact> contents, Iterable<ArchivedTreeArtifact> archivedTreeArtifacts) |
| 177 | throws IOException { |
| 178 | TreeArtifactValue.MultiBuilder treeArtifacts = TreeArtifactValue.newMultiBuilder(); |
| 179 | |
| 180 | for (TreeFileArtifact output : contents) { |
| 181 | treeArtifacts.putChild(output, createMetadataFromFileSystem(output)); |
| 182 | } |
| 183 | |
| 184 | for (ArchivedTreeArtifact archivedTreeArtifact : archivedTreeArtifacts) { |
| 185 | treeArtifacts.setArchivedRepresentation( |
| 186 | archivedTreeArtifact, createMetadataFromFileSystem(archivedTreeArtifact)); |
| 187 | } |
| 188 | |
| 189 | Map<Artifact, TreeArtifactValue> treeArtifactData = new HashMap<>(); |
| 190 | treeArtifacts.injectTo(treeArtifactData::put); |
| 191 | |
| 192 | return ActionExecutionValue.createForTesting( |
| 193 | /*artifactData=*/ ImmutableMap.of(), |
| 194 | ImmutableMap.copyOf(treeArtifactData), |
| 195 | /*outputSymlinks=*/ null); |
| 196 | } |
| 197 | |
| 198 | private static FileArtifactValue createMetadataFromFileSystem(Artifact artifact) |
| 199 | throws IOException { |
| 200 | Path path = artifact.getPath(); |
| 201 | FileArtifactValue noDigest = |
| 202 | ActionMetadataHandler.fileArtifactValueFromArtifact( |
janakr | 95278b4 | 2022-03-04 13:41:46 -0800 | [diff] [blame] | 203 | artifact, |
janakr | 61b3bb2 | 2022-03-07 10:11:00 -0800 | [diff] [blame] | 204 | FileStatusWithDigestAdapter.maybeAdapt(path.statIfFound(Symlinks.NOFOLLOW)), |
janakr | 95278b4 | 2022-03-04 13:41:46 -0800 | [diff] [blame] | 205 | SyscallCache.NO_CACHE, |
| 206 | null); |
Googler | fe72dbb | 2021-12-29 07:12:53 -0800 | [diff] [blame] | 207 | return FileArtifactValue.createFromInjectedDigest(noDigest, path.getDigest()); |
| 208 | } |
| 209 | |
| 210 | void writeFile(Path path, String... lines) throws IOException { |
| 211 | // Make sure we advance the clock to detect modifications which do not change the size, which |
| 212 | // rely on ctime. |
| 213 | fs.advanceClockMillis(1); |
| 214 | FileSystemUtils.writeIsoLatin1(path, lines); |
| 215 | } |
| 216 | |
| 217 | private static final class MockFileSystem extends InMemoryFileSystem { |
| 218 | boolean statThrowsRuntimeException; |
| 219 | boolean readlinkThrowsIoException; |
| 220 | |
| 221 | MockFileSystem() { |
| 222 | this(new ManualClock()); |
| 223 | } |
| 224 | |
| 225 | private MockFileSystem(ManualClock clock) { |
| 226 | super(clock, DigestHashFunction.SHA256); |
| 227 | } |
| 228 | |
| 229 | @Override |
| 230 | public FileStatus statIfFound(PathFragment path, boolean followSymlinks) throws IOException { |
| 231 | if (statThrowsRuntimeException) { |
| 232 | throw new RuntimeException("bork"); |
| 233 | } |
| 234 | return super.statIfFound(path, followSymlinks); |
| 235 | } |
| 236 | |
| 237 | @Override |
| 238 | protected PathFragment readSymbolicLink(PathFragment path) throws IOException { |
| 239 | if (readlinkThrowsIoException) { |
| 240 | throw new IOException("readlink failed"); |
| 241 | } |
| 242 | return super.readSymbolicLink(path); |
| 243 | } |
| 244 | |
| 245 | void advanceClockMillis(int millis) { |
| 246 | ((ManualClock) clock).advanceMillis(millis); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | private enum BatchStatMode { |
| 251 | DISABLED { |
| 252 | @Nullable |
| 253 | @Override |
| 254 | BatchStat getBatchStat(FileSystem fileSystem) { |
| 255 | return null; |
| 256 | } |
| 257 | }, |
| 258 | ENABLED { |
| 259 | @Override |
| 260 | BatchStat getBatchStat(FileSystem fileSystem) { |
| 261 | return (useDigest, includeLinks, paths) -> { |
| 262 | List<FileStatusWithDigest> stats = new ArrayList<>(); |
| 263 | for (PathFragment pathFrag : paths) { |
| 264 | stats.add( |
janakr | 61b3bb2 | 2022-03-07 10:11:00 -0800 | [diff] [blame] | 265 | FileStatusWithDigestAdapter.maybeAdapt( |
Googler | fe72dbb | 2021-12-29 07:12:53 -0800 | [diff] [blame] | 266 | fileSystem.getPath("/").getRelative(pathFrag).statIfFound(Symlinks.NOFOLLOW))); |
| 267 | } |
| 268 | return stats; |
| 269 | }; |
| 270 | } |
| 271 | }; |
| 272 | |
| 273 | @Nullable |
| 274 | abstract BatchStat getBatchStat(FileSystem fileSystem); |
| 275 | } |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 276 | |
Han-Wen Nienhuys | 3b2eae3 | 2015-10-28 16:35:08 +0000 | [diff] [blame] | 277 | @Before |
ajurkowski | fbd4427 | 2021-12-02 16:05:49 -0800 | [diff] [blame] | 278 | public void setUp() throws Exception { |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 279 | ImmutableMap.Builder<SkyFunctionName, SkyFunction> skyFunctions = ImmutableMap.builder(); |
| 280 | |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 281 | pkgRoot = fs.getPath("/testroot"); |
jhorvitz | 3daedc3 | 2020-07-22 18:33:55 -0700 | [diff] [blame] | 282 | pkgRoot.createDirectoryAndParents(); |
Kristina Chodorow | f9fdc8d | 2015-12-08 12:49:31 +0000 | [diff] [blame] | 283 | FileSystemUtils.createEmptyFile(pkgRoot.getRelative("WORKSPACE")); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 284 | |
John Cater | e0d1d0e | 2017-11-28 20:47:41 -0800 | [diff] [blame] | 285 | AtomicReference<PathPackageLocator> pkgLocator = |
| 286 | new AtomicReference<>( |
| 287 | new PathPackageLocator( |
| 288 | fs.getPath("/output_base"), |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 289 | ImmutableList.of(Root.fromPath(pkgRoot)), |
John Cater | e0d1d0e | 2017-11-28 20:47:41 -0800 | [diff] [blame] | 290 | BazelSkyframeExecutorConstants.BUILD_FILES_BY_PRIORITY)); |
janakr | 3b63a4e | 2017-09-14 09:55:40 +0200 | [diff] [blame] | 291 | BlazeDirectories directories = |
| 292 | new BlazeDirectories( |
cushon | 849df36 | 2018-05-14 01:51:45 -0700 | [diff] [blame] | 293 | new ServerDirectories(pkgRoot, pkgRoot, pkgRoot), |
| 294 | pkgRoot, |
| 295 | /* defaultSystemJavabase= */ null, |
| 296 | TestConstants.PRODUCT_NAME); |
nharmata | 3fb7d34 | 2018-02-23 11:37:51 -0800 | [diff] [blame] | 297 | ExternalFilesHelper externalFilesHelper = ExternalFilesHelper.createForTesting( |
Nathan Harmata | d4f7594 | 2016-10-18 08:55:17 +0000 | [diff] [blame] | 298 | pkgLocator, ExternalFileAction.DEPEND_ON_EXTERNAL_PKG_FOR_EXTERNAL_REPO_PATHS, directories); |
shahan | 602cc85 | 2018-06-06 20:09:57 -0700 | [diff] [blame] | 299 | skyFunctions.put( |
nharmata | d533996 | 2022-02-11 14:44:08 -0800 | [diff] [blame] | 300 | FileStateKey.FILE_STATE, |
shahan | 602cc85 | 2018-06-06 20:09:57 -0700 | [diff] [blame] | 301 | new FileStateFunction( |
janakr | fc1d79c | 2022-01-27 13:02:07 -0800 | [diff] [blame] | 302 | Suppliers.ofInstance(new TimestampGranularityMonitor(BlazeClock.instance())), |
janakr | b2a9434 | 2022-02-05 22:02:14 -0800 | [diff] [blame] | 303 | SyscallCache.NO_CACHE, |
djasper | 7faa0ef | 2019-03-28 10:00:00 -0700 | [diff] [blame] | 304 | externalFilesHelper)); |
shahan | 602cc85 | 2018-06-06 20:09:57 -0700 | [diff] [blame] | 305 | skyFunctions.put(FileValue.FILE, new FileFunction(pkgLocator)); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 306 | skyFunctions.put( |
janakr | e2af68f | 2021-03-18 15:11:30 -0700 | [diff] [blame] | 307 | FileSymlinkCycleUniquenessFunction.NAME, new FileSymlinkCycleUniquenessFunction()); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 308 | skyFunctions.put( |
janakr | e2af68f | 2021-03-18 15:11:30 -0700 | [diff] [blame] | 309 | FileSymlinkInfiniteExpansionUniquenessFunction.NAME, |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 310 | new FileSymlinkInfiniteExpansionUniquenessFunction()); |
ajurkowski | 2018b87 | 2020-04-14 09:59:00 -0700 | [diff] [blame] | 311 | skyFunctions.put( |
janakr | e4fcd47 | 2021-05-26 08:47:39 -0700 | [diff] [blame] | 312 | SkyFunctions.PACKAGE, |
| 313 | new PackageFunction( |
| 314 | null, |
| 315 | null, |
| 316 | null, |
| 317 | null, |
| 318 | null, |
janakr | e4fcd47 | 2021-05-26 08:47:39 -0700 | [diff] [blame] | 319 | /*packageProgress=*/ null, |
| 320 | PackageFunction.ActionOnIOExceptionReadingBuildFile.UseOriginalIOException.INSTANCE, |
jhorvitz | 1d675d1 | 2022-04-28 16:23:21 -0700 | [diff] [blame^] | 321 | GlobbingStrategy.SKYFRAME_HYBRID, |
janakr | 13b737a | 2021-07-02 14:24:25 -0700 | [diff] [blame] | 322 | k -> ThreadStateReceiver.NULL_INSTANCE)); |
John Cater | 5e9ce94 | 2016-10-12 17:23:30 +0000 | [diff] [blame] | 323 | skyFunctions.put( |
| 324 | SkyFunctions.PACKAGE_LOOKUP, |
| 325 | new PackageLookupFunction( |
jhorvitz | 3daedc3 | 2020-07-22 18:33:55 -0700 | [diff] [blame] | 326 | new AtomicReference<>(ImmutableSet.of()), |
John Cater | 0c0735a | 2016-11-11 01:52:02 +0000 | [diff] [blame] | 327 | CrossRepositoryLabelViolationStrategy.ERROR, |
ajurkowski | 2018b87 | 2020-04-14 09:59:00 -0700 | [diff] [blame] | 328 | BazelSkyframeExecutorConstants.BUILD_FILES_BY_PRIORITY, |
| 329 | BazelSkyframeExecutorConstants.EXTERNAL_PACKAGE_HELPER)); |
carmi | d6a9828 | 2018-03-13 19:19:16 -0700 | [diff] [blame] | 330 | skyFunctions.put( |
janakr | 15e15c2 | 2019-01-30 11:24:49 -0800 | [diff] [blame] | 331 | WorkspaceFileValue.WORKSPACE_FILE, |
carmi | d6a9828 | 2018-03-13 19:19:16 -0700 | [diff] [blame] | 332 | new WorkspaceFileFunction( |
| 333 | TestRuleClassProvider.getRuleClassProvider(), |
michajlo | 8083e32 | 2020-03-20 13:32:52 -0700 | [diff] [blame] | 334 | TestPackageFactoryBuilderFactory.getInstance() |
carmi | d6a9828 | 2018-03-13 19:19:16 -0700 | [diff] [blame] | 335 | .builder(directories) |
nharmata | fde0bd2f | 2018-12-21 10:17:56 -0800 | [diff] [blame] | 336 | .build(TestRuleClassProvider.getRuleClassProvider(), fs), |
mjhalupka | f0e4811 | 2019-01-14 13:01:56 -0800 | [diff] [blame] | 337 | directories, |
brandjon | 771a029 | 2020-05-26 12:04:16 -0700 | [diff] [blame] | 338 | /*bzlLoadFunctionForInlining=*/ null)); |
ajurkowski | 2018b87 | 2020-04-14 09:59:00 -0700 | [diff] [blame] | 339 | skyFunctions.put( |
| 340 | SkyFunctions.EXTERNAL_PACKAGE, |
| 341 | new ExternalPackageFunction(BazelSkyframeExecutorConstants.EXTERNAL_PACKAGE_HELPER)); |
Kristina Chodorow | f9fdc8d | 2015-12-08 12:49:31 +0000 | [diff] [blame] | 342 | |
janakr | 1cde872 | 2017-10-10 03:22:21 +0200 | [diff] [blame] | 343 | differencer = new SequencedRecordingDifferencer(); |
jhorvitz | 7f55cb7 | 2021-12-16 18:52:24 -0800 | [diff] [blame] | 344 | evaluator = new InMemoryMemoizingEvaluator(skyFunctions.buildOrThrow(), differencer); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 345 | PrecomputedValue.BUILD_ID.set(differencer, UUID.randomUUID()); |
Kristina Chodorow | f9fdc8d | 2015-12-08 12:49:31 +0000 | [diff] [blame] | 346 | PrecomputedValue.PATH_PACKAGE_LOCATOR.set(differencer, pkgLocator.get()); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 347 | } |
| 348 | |
ajurkowski | 64795c2 | 2022-04-01 10:38:44 -0700 | [diff] [blame] | 349 | @Before |
| 350 | public void setupModifiedOutputReceiverMock() { |
| 351 | doNothing() |
| 352 | .when(mockModifiedOutputsReceiver) |
| 353 | .reportModifiedOutputFile(anyLong(), modifiedOutputsCaptor.capture()); |
| 354 | } |
| 355 | |
Googler | fe72dbb | 2021-12-29 07:12:53 -0800 | [diff] [blame] | 356 | public static ImmutableList<Object[]> batchStatModes() { |
| 357 | return Arrays.stream(BatchStatMode.values()) |
| 358 | .map(mode -> new BatchStatMode[] {mode}) |
| 359 | .collect(toImmutableList()); |
| 360 | } |
| 361 | |
| 362 | @Test |
| 363 | public void getDirtyActionValues_unchangedEmptyTreeArtifactWithArchivedFile_noDirtyKeys() |
| 364 | throws Exception { |
| 365 | SpecialArtifact treeArtifact = createTreeArtifact("dir"); |
| 366 | treeArtifact.getPath().createDirectoryAndParents(); |
| 367 | ActionExecutionValue actionExecutionValue = |
| 368 | actionValueWithTreeArtifacts( |
| 369 | ImmutableList.of(), |
| 370 | ImmutableList.of(createArchivedTreeArtifactWithContent(treeArtifact))); |
| 371 | |
| 372 | assertThat(getDirtyActionValues(actionExecutionValue)).isEmpty(); |
| 373 | } |
| 374 | |
| 375 | @Test |
| 376 | public void getDirtyActionValues_unchangedTreeArtifactWithArchivedFile_noDirtyKeys() |
| 377 | throws Exception { |
| 378 | SpecialArtifact treeArtifact = createTreeArtifact("dir"); |
| 379 | ActionExecutionValue actionExecutionValue = |
| 380 | actionValueWithTreeArtifacts( |
| 381 | ImmutableList.of( |
| 382 | createTreeFileArtifactWithContent(treeArtifact, "file1", "content"), |
| 383 | createTreeFileArtifactWithContent(treeArtifact, "file2", "content2")), |
| 384 | ImmutableList.of(createArchivedTreeArtifactWithContent(treeArtifact))); |
| 385 | |
| 386 | assertThat(getDirtyActionValues(actionExecutionValue)).isEmpty(); |
| 387 | } |
| 388 | |
| 389 | @Test |
| 390 | public void getDirtyActionValues_editedArchivedFileForEmptyTreeArtifact_reportsChange() |
| 391 | throws Exception { |
| 392 | SpecialArtifact treeArtifact = createTreeArtifact("dir"); |
| 393 | treeArtifact.getPath().createDirectoryAndParents(); |
| 394 | ArchivedTreeArtifact archivedTreeArtifact = |
| 395 | createArchivedTreeArtifactWithContent(treeArtifact, "old content"); |
| 396 | ActionExecutionValue actionExecutionValue = |
| 397 | actionValueWithTreeArtifacts(ImmutableList.of(), ImmutableList.of(archivedTreeArtifact)); |
| 398 | |
| 399 | writeFile(archivedTreeArtifact.getPath(), "new content"); |
| 400 | assertThat(getDirtyActionValues(actionExecutionValue)).containsExactly(ACTION_LOOKUP_DATA); |
| 401 | } |
| 402 | |
| 403 | @Test |
| 404 | public void getDirtyActionValues_editedArchivedFileForTreeArtifact_reportsChange() |
| 405 | throws Exception { |
| 406 | SpecialArtifact treeArtifact = createTreeArtifact("dir"); |
| 407 | ArchivedTreeArtifact archivedTreeArtifact = |
| 408 | createArchivedTreeArtifactWithContent(treeArtifact, "old content"); |
| 409 | ActionExecutionValue actionExecutionValue = |
| 410 | actionValueWithTreeArtifacts( |
| 411 | ImmutableList.of( |
| 412 | createTreeFileArtifactWithContent( |
| 413 | treeArtifact, /*parentRelativePath=*/ "file1", "content"), |
| 414 | createTreeFileArtifactWithContent( |
| 415 | treeArtifact, /*parentRelativePath=*/ "file2", "content2")), |
| 416 | ImmutableList.of(archivedTreeArtifact)); |
| 417 | |
| 418 | writeFile(archivedTreeArtifact.getPath(), "new content"); |
| 419 | assertThat(getDirtyActionValues(actionExecutionValue)).containsExactly(ACTION_LOOKUP_DATA); |
| 420 | } |
| 421 | |
| 422 | @Test |
| 423 | public void getDirtyActionValues_deletedArchivedFileForTreeArtifact_reportsChange() |
| 424 | throws Exception { |
| 425 | SpecialArtifact treeArtifact = createTreeArtifact("dir"); |
| 426 | ArchivedTreeArtifact archivedTreeArtifact = createArchivedTreeArtifactWithContent(treeArtifact); |
| 427 | ActionExecutionValue actionExecutionValue = |
| 428 | actionValueWithTreeArtifacts( |
| 429 | ImmutableList.of( |
| 430 | createTreeFileArtifactWithContent( |
| 431 | treeArtifact, /*parentRelativePath=*/ "file1", "content"), |
| 432 | createTreeFileArtifactWithContent( |
| 433 | treeArtifact, /*parentRelativePath=*/ "file2", "content2")), |
| 434 | ImmutableList.of(archivedTreeArtifact)); |
| 435 | |
| 436 | archivedTreeArtifact.getPath().delete(); |
| 437 | assertThat(getDirtyActionValues(actionExecutionValue)).containsExactly(ACTION_LOOKUP_DATA); |
| 438 | } |
| 439 | |
| 440 | @Test |
| 441 | public void getDirtyActionValues_deletedArchivedFileForEmptyTreeArtifact_reportsChange() |
| 442 | throws Exception { |
| 443 | SpecialArtifact treeArtifact = createTreeArtifact("dir"); |
| 444 | ArchivedTreeArtifact archivedTreeArtifact = createArchivedTreeArtifactWithContent(treeArtifact); |
| 445 | ActionExecutionValue actionExecutionValue = |
| 446 | actionValueWithTreeArtifacts(ImmutableList.of(), ImmutableList.of(archivedTreeArtifact)); |
| 447 | |
| 448 | archivedTreeArtifact.getPath().delete(); |
| 449 | assertThat(getDirtyActionValues(actionExecutionValue)).containsExactly(ACTION_LOOKUP_DATA); |
| 450 | } |
| 451 | |
| 452 | @Test |
| 453 | public void getDirtyActionValues_editedFileForTreeArtifactWithArchivedFile_reportsChange() |
| 454 | throws Exception { |
| 455 | SpecialArtifact treeArtifact = createTreeArtifact("dir"); |
| 456 | TreeFileArtifact child1 = |
| 457 | createTreeFileArtifactWithContent( |
| 458 | treeArtifact, /*parentRelativePath=*/ "file1", "old content"); |
| 459 | ActionExecutionValue actionExecutionValue = |
| 460 | actionValueWithTreeArtifacts( |
| 461 | ImmutableList.of( |
| 462 | child1, |
| 463 | createTreeFileArtifactWithContent( |
| 464 | treeArtifact, /*parentRelativePath=*/ "file2", "content2")), |
| 465 | ImmutableList.of(createArchivedTreeArtifactWithContent(treeArtifact))); |
| 466 | |
| 467 | writeFile(child1.getPath(), "new content"); |
| 468 | assertThat(getDirtyActionValues(actionExecutionValue)).containsExactly(ACTION_LOOKUP_DATA); |
| 469 | } |
| 470 | |
| 471 | @Test |
| 472 | public void getDirtyActionValues_treeArtifactWithArchivedArtifact_reportsOnlyChangedKey() |
| 473 | throws Exception { |
| 474 | SpecialArtifact unchangedTreeArtifact = createTreeArtifact("dir1"); |
| 475 | ActionExecutionValue unchangedValue = |
| 476 | actionValueWithTreeArtifacts( |
| 477 | ImmutableList.of(createTreeFileArtifactWithContent(unchangedTreeArtifact, "child")), |
| 478 | ImmutableList.of(createArchivedTreeArtifactWithContent(unchangedTreeArtifact))); |
| 479 | SpecialArtifact changedTreeArtifact = createTreeArtifact("dir2"); |
| 480 | ArchivedTreeArtifact changedArchivedTreeArtifact = |
| 481 | createArchivedTreeArtifactWithContent(changedTreeArtifact, "old content"); |
| 482 | ActionExecutionValue changedValue = |
| 483 | actionValueWithTreeArtifacts( |
| 484 | ImmutableList.of( |
| 485 | createTreeFileArtifactWithContent(changedTreeArtifact, "file", "content")), |
| 486 | ImmutableList.of(changedArchivedTreeArtifact)); |
| 487 | |
| 488 | writeFile(changedArchivedTreeArtifact.getPath(), "new content"); |
| 489 | assertThat( |
| 490 | getDirtyActionValues( |
| 491 | ImmutableMap.of( |
| 492 | actionLookupData(0), unchangedValue, actionLookupData(1), changedValue))) |
| 493 | .containsExactly(actionLookupData(1)); |
| 494 | } |
| 495 | |
| 496 | private Collection<SkyKey> getDirtyActionValues(ActionExecutionValue actionExecutionValue) |
| 497 | throws InterruptedException { |
| 498 | return getDirtyActionValues(ImmutableMap.of(ACTION_LOOKUP_DATA, actionExecutionValue)); |
| 499 | } |
| 500 | |
| 501 | private Collection<SkyKey> getDirtyActionValues(ImmutableMap<SkyKey, SkyValue> valuesMap) |
| 502 | throws InterruptedException { |
ajurkowski | 64795c2 | 2022-04-01 10:38:44 -0700 | [diff] [blame] | 503 | return new FilesystemValueChecker(/*tsgm=*/ null, SyscallCache.NO_CACHE, FSVC_THREADS_FOR_TEST) |
Googler | fe72dbb | 2021-12-29 07:12:53 -0800 | [diff] [blame] | 504 | .getDirtyActionValues( |
| 505 | valuesMap, |
| 506 | batchStat.getBatchStat(fs), |
| 507 | ModifiedFileSet.EVERYTHING_MODIFIED, |
ajurkowski | 64795c2 | 2022-04-01 10:38:44 -0700 | [diff] [blame] | 508 | /*trustRemoteArtifacts=*/ false, |
| 509 | (ignored, ignored2) -> {}); |
Googler | fe72dbb | 2021-12-29 07:12:53 -0800 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | private TreeFileArtifact createTreeFileArtifactWithContent( |
| 513 | SpecialArtifact treeArtifact, String parentRelativePath, String... contentLines) |
| 514 | throws IOException { |
| 515 | TreeFileArtifact artifact = TreeFileArtifact.createTreeOutput(treeArtifact, parentRelativePath); |
| 516 | writeFile(artifact.getPath(), contentLines); |
| 517 | return artifact; |
| 518 | } |
| 519 | |
| 520 | private ArchivedTreeArtifact createArchivedTreeArtifactWithContent( |
| 521 | SpecialArtifact treeArtifact, String... contentLines) throws IOException { |
| 522 | ArchivedTreeArtifact artifact = ArchivedTreeArtifact.createForTree(treeArtifact); |
| 523 | writeFile(artifact.getPath(), contentLines); |
| 524 | return artifact; |
| 525 | } |
| 526 | |
| 527 | private static ActionLookupData actionLookupData(int actionIndex) { |
| 528 | return ActionLookupData.create(ACTION_LOOKUP_KEY, actionIndex); |
| 529 | } |
| 530 | |
Han-Wen Nienhuys | 3b2eae3 | 2015-10-28 16:35:08 +0000 | [diff] [blame] | 531 | @Test |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 532 | public void testEmpty() throws Exception { |
twerth | 5aaceb5 | 2020-04-07 06:31:56 -0700 | [diff] [blame] | 533 | FilesystemValueChecker checker = |
| 534 | new FilesystemValueChecker( |
janakr | 873bf0a | 2022-03-04 13:38:50 -0800 | [diff] [blame] | 535 | /*tsgm=*/ null, |
| 536 | SyscallCache.NO_CACHE, |
janakr | 873bf0a | 2022-03-04 13:38:50 -0800 | [diff] [blame] | 537 | FSVC_THREADS_FOR_TEST); |
Nathan Harmata | 8cd2978 | 2015-11-10 03:24:01 +0000 | [diff] [blame] | 538 | assertEmptyDiff(getDirtyFilesystemKeys(evaluator, checker)); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 539 | } |
| 540 | |
Han-Wen Nienhuys | 3b2eae3 | 2015-10-28 16:35:08 +0000 | [diff] [blame] | 541 | @Test |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 542 | public void testSimple() throws Exception { |
twerth | 5aaceb5 | 2020-04-07 06:31:56 -0700 | [diff] [blame] | 543 | FilesystemValueChecker checker = |
| 544 | new FilesystemValueChecker( |
janakr | 873bf0a | 2022-03-04 13:38:50 -0800 | [diff] [blame] | 545 | /*tsgm=*/ null, |
| 546 | SyscallCache.NO_CACHE, |
janakr | 873bf0a | 2022-03-04 13:38:50 -0800 | [diff] [blame] | 547 | FSVC_THREADS_FOR_TEST); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 548 | |
| 549 | Path path = fs.getPath("/foo"); |
| 550 | FileSystemUtils.createEmptyFile(path); |
Nathan Harmata | 8cd2978 | 2015-11-10 03:24:01 +0000 | [diff] [blame] | 551 | assertEmptyDiff(getDirtyFilesystemKeys(evaluator, checker)); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 552 | |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 553 | SkyKey skyKey = |
| 554 | FileStateValue.key( |
tomlu | 4c9fafd | 2018-01-18 10:29:11 -0800 | [diff] [blame] | 555 | RootedPath.toRootedPath(Root.absoluteRoot(fs), PathFragment.create("/foo"))); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 556 | EvaluationResult<SkyValue> result = |
jhorvitz | 9ad8918 | 2021-12-29 11:57:02 -0800 | [diff] [blame] | 557 | evaluator.evaluate(ImmutableList.of(skyKey), EVALUATION_OPTIONS); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 558 | assertThat(result.hasError()).isFalse(); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 559 | |
Nathan Harmata | 8cd2978 | 2015-11-10 03:24:01 +0000 | [diff] [blame] | 560 | assertEmptyDiff(getDirtyFilesystemKeys(evaluator, checker)); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 561 | |
| 562 | FileSystemUtils.writeContentAsLatin1(path, "hello"); |
Nathan Harmata | 8cd2978 | 2015-11-10 03:24:01 +0000 | [diff] [blame] | 563 | assertDiffWithNewValues(getDirtyFilesystemKeys(evaluator, checker), skyKey); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 564 | |
| 565 | // The dirty bits are not reset until the FileValues are actually revalidated. |
Nathan Harmata | 8cd2978 | 2015-11-10 03:24:01 +0000 | [diff] [blame] | 566 | assertDiffWithNewValues(getDirtyFilesystemKeys(evaluator, checker), skyKey); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 567 | |
| 568 | differencer.invalidate(ImmutableList.of(skyKey)); |
jhorvitz | 9ad8918 | 2021-12-29 11:57:02 -0800 | [diff] [blame] | 569 | result = evaluator.evaluate(ImmutableList.of(skyKey), EVALUATION_OPTIONS); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 570 | assertThat(result.hasError()).isFalse(); |
Nathan Harmata | 8cd2978 | 2015-11-10 03:24:01 +0000 | [diff] [blame] | 571 | assertEmptyDiff(getDirtyFilesystemKeys(evaluator, checker)); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | /** |
| 575 | * Tests that an already-invalidated value can still be marked changed: symlink points at sym1. |
| 576 | * Invalidate symlink by changing sym1 from pointing at path to point to sym2. This only dirties |
| 577 | * (rather than changes) symlink because sym2 still points at path, so all symlink stats remain |
| 578 | * the same. Then do a null build, change sym1 back to point at path, and change symlink to not be |
| 579 | * a symlink anymore. The fact that it is not a symlink should be detected. |
| 580 | */ |
Han-Wen Nienhuys | 3b2eae3 | 2015-10-28 16:35:08 +0000 | [diff] [blame] | 581 | @Test |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 582 | public void testDirtySymlink() throws Exception { |
twerth | 5aaceb5 | 2020-04-07 06:31:56 -0700 | [diff] [blame] | 583 | FilesystemValueChecker checker = |
| 584 | new FilesystemValueChecker( |
janakr | 873bf0a | 2022-03-04 13:38:50 -0800 | [diff] [blame] | 585 | /*tsgm=*/ null, |
| 586 | SyscallCache.NO_CACHE, |
janakr | 873bf0a | 2022-03-04 13:38:50 -0800 | [diff] [blame] | 587 | FSVC_THREADS_FOR_TEST); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 588 | |
| 589 | Path path = fs.getPath("/foo"); |
| 590 | FileSystemUtils.writeContentAsLatin1(path, "foo contents"); |
| 591 | // We need the intermediate sym1 and sym2 so that we can dirty a child of symlink without |
| 592 | // actually changing the FileValue calculated for symlink (if we changed the contents of foo, |
Laszlo Csomor | a278aec | 2018-03-09 04:07:31 -0800 | [diff] [blame] | 593 | // the FileValue created for symlink would notice, since it stats foo). |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 594 | Path sym1 = fs.getPath("/sym1"); |
| 595 | Path sym2 = fs.getPath("/sym2"); |
| 596 | Path symlink = fs.getPath("/bar"); |
| 597 | FileSystemUtils.ensureSymbolicLink(symlink, sym1); |
| 598 | FileSystemUtils.ensureSymbolicLink(sym1, path); |
| 599 | FileSystemUtils.ensureSymbolicLink(sym2, path); |
| 600 | SkyKey fooKey = |
tomlu | 4c9fafd | 2018-01-18 10:29:11 -0800 | [diff] [blame] | 601 | FileValue.key(RootedPath.toRootedPath(Root.absoluteRoot(fs), PathFragment.create("/foo"))); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 602 | RootedPath symlinkRootedPath = |
tomlu | 4c9fafd | 2018-01-18 10:29:11 -0800 | [diff] [blame] | 603 | RootedPath.toRootedPath(Root.absoluteRoot(fs), PathFragment.create("/bar")); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 604 | SkyKey symlinkKey = FileValue.key(symlinkRootedPath); |
| 605 | SkyKey symlinkFileStateKey = FileStateValue.key(symlinkRootedPath); |
| 606 | RootedPath sym1RootedPath = |
tomlu | 4c9fafd | 2018-01-18 10:29:11 -0800 | [diff] [blame] | 607 | RootedPath.toRootedPath(Root.absoluteRoot(fs), PathFragment.create("/sym1")); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 608 | SkyKey sym1FileStateKey = FileStateValue.key(sym1RootedPath); |
| 609 | Iterable<SkyKey> allKeys = ImmutableList.of(symlinkKey, fooKey); |
| 610 | |
| 611 | // First build -- prime the graph. |
jhorvitz | 9ad8918 | 2021-12-29 11:57:02 -0800 | [diff] [blame] | 612 | EvaluationResult<FileValue> result = evaluator.evaluate(allKeys, EVALUATION_OPTIONS); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 613 | assertThat(result.hasError()).isFalse(); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 614 | FileValue symlinkValue = result.get(symlinkKey); |
| 615 | FileValue fooValue = result.get(fooKey); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 616 | assertWithMessage(symlinkValue.toString()).that(symlinkValue.isSymlink()).isTrue(); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 617 | // Digest is not always available, so use size as a proxy for contents. |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 618 | assertThat(symlinkValue.getSize()).isEqualTo(fooValue.getSize()); |
Nathan Harmata | 8cd2978 | 2015-11-10 03:24:01 +0000 | [diff] [blame] | 619 | assertEmptyDiff(getDirtyFilesystemKeys(evaluator, checker)); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 620 | |
| 621 | // Before second build, move sym1 to point to sym2. |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 622 | assertThat(sym1.delete()).isTrue(); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 623 | FileSystemUtils.ensureSymbolicLink(sym1, sym2); |
Nathan Harmata | 8cd2978 | 2015-11-10 03:24:01 +0000 | [diff] [blame] | 624 | assertDiffWithNewValues(getDirtyFilesystemKeys(evaluator, checker), sym1FileStateKey); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 625 | |
| 626 | differencer.invalidate(ImmutableList.of(sym1FileStateKey)); |
jhorvitz | 9ad8918 | 2021-12-29 11:57:02 -0800 | [diff] [blame] | 627 | result = evaluator.evaluate(ImmutableList.of(), EVALUATION_OPTIONS); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 628 | assertThat(result.hasError()).isFalse(); |
Nathan Harmata | 8cd2978 | 2015-11-10 03:24:01 +0000 | [diff] [blame] | 629 | assertDiffWithNewValues(getDirtyFilesystemKeys(evaluator, checker), sym1FileStateKey); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 630 | |
| 631 | // Before third build, move sym1 back to original (so change pruning will prevent signaling of |
| 632 | // its parents, but change symlink for real. |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 633 | assertThat(sym1.delete()).isTrue(); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 634 | FileSystemUtils.ensureSymbolicLink(sym1, path); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 635 | assertThat(symlink.delete()).isTrue(); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 636 | FileSystemUtils.writeContentAsLatin1(symlink, "new symlink contents"); |
Nathan Harmata | 8cd2978 | 2015-11-10 03:24:01 +0000 | [diff] [blame] | 637 | assertDiffWithNewValues(getDirtyFilesystemKeys(evaluator, checker), symlinkFileStateKey); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 638 | differencer.invalidate(ImmutableList.of(symlinkFileStateKey)); |
jhorvitz | 9ad8918 | 2021-12-29 11:57:02 -0800 | [diff] [blame] | 639 | result = evaluator.evaluate(allKeys, EVALUATION_OPTIONS); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 640 | assertThat(result.hasError()).isFalse(); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 641 | symlinkValue = result.get(symlinkKey); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 642 | assertWithMessage(symlinkValue.toString()).that(symlinkValue.isSymlink()).isFalse(); |
| 643 | assertThat(result.get(fooKey)).isEqualTo(fooValue); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 644 | assertThat(symlinkValue.getSize()).isNotEqualTo(fooValue.getSize()); |
Nathan Harmata | 8cd2978 | 2015-11-10 03:24:01 +0000 | [diff] [blame] | 645 | assertEmptyDiff(getDirtyFilesystemKeys(evaluator, checker)); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 646 | } |
| 647 | |
Han-Wen Nienhuys | 3b2eae3 | 2015-10-28 16:35:08 +0000 | [diff] [blame] | 648 | @Test |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 649 | public void testExplicitFiles() throws Exception { |
twerth | 5aaceb5 | 2020-04-07 06:31:56 -0700 | [diff] [blame] | 650 | FilesystemValueChecker checker = |
| 651 | new FilesystemValueChecker( |
janakr | 873bf0a | 2022-03-04 13:38:50 -0800 | [diff] [blame] | 652 | /*tsgm=*/ null, |
| 653 | SyscallCache.NO_CACHE, |
janakr | 873bf0a | 2022-03-04 13:38:50 -0800 | [diff] [blame] | 654 | FSVC_THREADS_FOR_TEST); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 655 | |
| 656 | Path path1 = fs.getPath("/foo1"); |
| 657 | Path path2 = fs.getPath("/foo2"); |
| 658 | FileSystemUtils.createEmptyFile(path1); |
| 659 | FileSystemUtils.createEmptyFile(path2); |
Nathan Harmata | 8cd2978 | 2015-11-10 03:24:01 +0000 | [diff] [blame] | 660 | assertEmptyDiff(getDirtyFilesystemKeys(evaluator, checker)); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 661 | |
| 662 | SkyKey key1 = |
| 663 | FileStateValue.key( |
tomlu | 4c9fafd | 2018-01-18 10:29:11 -0800 | [diff] [blame] | 664 | RootedPath.toRootedPath(Root.absoluteRoot(fs), PathFragment.create("/foo1"))); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 665 | SkyKey key2 = |
| 666 | FileStateValue.key( |
tomlu | 4c9fafd | 2018-01-18 10:29:11 -0800 | [diff] [blame] | 667 | RootedPath.toRootedPath(Root.absoluteRoot(fs), PathFragment.create("/foo2"))); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 668 | Iterable<SkyKey> skyKeys = ImmutableList.of(key1, key2); |
jhorvitz | 9ad8918 | 2021-12-29 11:57:02 -0800 | [diff] [blame] | 669 | EvaluationResult<SkyValue> result = evaluator.evaluate(skyKeys, EVALUATION_OPTIONS); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 670 | assertThat(result.hasError()).isFalse(); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 671 | |
Nathan Harmata | 8cd2978 | 2015-11-10 03:24:01 +0000 | [diff] [blame] | 672 | assertEmptyDiff(getDirtyFilesystemKeys(evaluator, checker)); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 673 | |
Laszlo Csomor | a278aec | 2018-03-09 04:07:31 -0800 | [diff] [blame] | 674 | // Wait for the timestamp granularity to elapse, so updating the files will observably advance |
| 675 | // their ctime. |
laszlocsomor | 29bdf63 | 2018-03-13 02:50:46 -0700 | [diff] [blame] | 676 | TimestampGranularityUtils.waitForTimestampGranularity( |
| 677 | System.currentTimeMillis(), OutErr.SYSTEM_OUT_ERR); |
ajurkowski | 280bbe2 | 2020-08-19 11:26:20 -0700 | [diff] [blame] | 678 | // Update path1's contents. This will update the file's ctime with current time indicated by the |
| 679 | // clock. |
| 680 | fs.advanceClockMillis(1); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 681 | FileSystemUtils.writeContentAsLatin1(path1, "hello1"); |
Laszlo Csomor | a278aec | 2018-03-09 04:07:31 -0800 | [diff] [blame] | 682 | // Update path2's mtime but not its contents. We expect that an mtime change suffices to update |
| 683 | // the ctime. |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 684 | path2.setLastModifiedTime(42); |
Laszlo Csomor | a278aec | 2018-03-09 04:07:31 -0800 | [diff] [blame] | 685 | // Assert that both files changed. The change detection relies, among other things, on ctime |
| 686 | // change. |
Nathan Harmata | 8cd2978 | 2015-11-10 03:24:01 +0000 | [diff] [blame] | 687 | assertDiffWithNewValues(getDirtyFilesystemKeys(evaluator, checker), key1, key2); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 688 | |
| 689 | differencer.invalidate(skyKeys); |
jhorvitz | 9ad8918 | 2021-12-29 11:57:02 -0800 | [diff] [blame] | 690 | result = evaluator.evaluate(skyKeys, EVALUATION_OPTIONS); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 691 | assertThat(result.hasError()).isFalse(); |
Nathan Harmata | 8cd2978 | 2015-11-10 03:24:01 +0000 | [diff] [blame] | 692 | assertEmptyDiff(getDirtyFilesystemKeys(evaluator, checker)); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 693 | } |
| 694 | |
Han-Wen Nienhuys | 3b2eae3 | 2015-10-28 16:35:08 +0000 | [diff] [blame] | 695 | @Test |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 696 | public void testFileWithIOExceptionNotConsideredDirty() throws Exception { |
| 697 | Path path = fs.getPath("/testroot/foo"); |
| 698 | path.getParentDirectory().createDirectory(); |
nharmata | b4060b6 | 2017-04-04 17:11:39 +0000 | [diff] [blame] | 699 | path.createSymbolicLink(PathFragment.create("bar")); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 700 | |
| 701 | fs.readlinkThrowsIoException = true; |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 702 | SkyKey fileKey = |
| 703 | FileStateValue.key( |
| 704 | RootedPath.toRootedPath(Root.fromPath(pkgRoot), PathFragment.create("foo"))); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 705 | EvaluationResult<SkyValue> result = |
jhorvitz | 9ad8918 | 2021-12-29 11:57:02 -0800 | [diff] [blame] | 706 | evaluator.evaluate(ImmutableList.of(fileKey), EVALUATION_OPTIONS); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 707 | assertThat(result.hasError()).isTrue(); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 708 | |
| 709 | fs.readlinkThrowsIoException = false; |
twerth | 5aaceb5 | 2020-04-07 06:31:56 -0700 | [diff] [blame] | 710 | FilesystemValueChecker checker = |
| 711 | new FilesystemValueChecker( |
janakr | 873bf0a | 2022-03-04 13:38:50 -0800 | [diff] [blame] | 712 | /*tsgm=*/ null, |
| 713 | SyscallCache.NO_CACHE, |
janakr | 873bf0a | 2022-03-04 13:38:50 -0800 | [diff] [blame] | 714 | FSVC_THREADS_FOR_TEST); |
Nathan Harmata | 8cd2978 | 2015-11-10 03:24:01 +0000 | [diff] [blame] | 715 | Diff diff = getDirtyFilesystemKeys(evaluator, checker); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 716 | assertThat(diff.changedKeysWithoutNewValues()).isEmpty(); |
| 717 | assertThat(diff.changedKeysWithNewValues()).isEmpty(); |
| 718 | } |
| 719 | |
Han-Wen Nienhuys | 3b2eae3 | 2015-10-28 16:35:08 +0000 | [diff] [blame] | 720 | @Test |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 721 | public void testFilesInCycleNotConsideredDirty() throws Exception { |
| 722 | Path path1 = pkgRoot.getRelative("foo1"); |
| 723 | Path path2 = pkgRoot.getRelative("foo2"); |
| 724 | Path path3 = pkgRoot.getRelative("foo3"); |
| 725 | FileSystemUtils.ensureSymbolicLink(path1, path2); |
| 726 | FileSystemUtils.ensureSymbolicLink(path2, path3); |
| 727 | FileSystemUtils.ensureSymbolicLink(path3, path1); |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 728 | SkyKey fileKey1 = FileValue.key(RootedPath.toRootedPath(Root.fromPath(pkgRoot), path1)); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 729 | |
| 730 | EvaluationResult<SkyValue> result = |
jhorvitz | 9ad8918 | 2021-12-29 11:57:02 -0800 | [diff] [blame] | 731 | evaluator.evaluate(ImmutableList.of(fileKey1), EVALUATION_OPTIONS); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 732 | assertThat(result.hasError()).isTrue(); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 733 | |
twerth | 5aaceb5 | 2020-04-07 06:31:56 -0700 | [diff] [blame] | 734 | FilesystemValueChecker checker = |
| 735 | new FilesystemValueChecker( |
janakr | 873bf0a | 2022-03-04 13:38:50 -0800 | [diff] [blame] | 736 | /*tsgm=*/ null, |
| 737 | SyscallCache.NO_CACHE, |
janakr | 873bf0a | 2022-03-04 13:38:50 -0800 | [diff] [blame] | 738 | FSVC_THREADS_FOR_TEST); |
Nathan Harmata | 8cd2978 | 2015-11-10 03:24:01 +0000 | [diff] [blame] | 739 | Diff diff = getDirtyFilesystemKeys(evaluator, checker); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 740 | assertThat(diff.changedKeysWithoutNewValues()).isEmpty(); |
| 741 | assertThat(diff.changedKeysWithNewValues()).isEmpty(); |
| 742 | } |
| 743 | |
jhorvitz | 3daedc3 | 2020-07-22 18:33:55 -0700 | [diff] [blame] | 744 | public void checkDirtyActions(BatchStat batchStatter) throws Exception { |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 745 | Artifact out1 = createDerivedArtifact("fiz"); |
| 746 | Artifact out2 = createDerivedArtifact("pop"); |
| 747 | |
| 748 | FileSystemUtils.writeContentAsLatin1(out1.getPath(), "hello"); |
| 749 | FileSystemUtils.writeContentAsLatin1(out2.getPath(), "fizzlepop"); |
| 750 | |
lberki | 41cfcc7 | 2019-08-05 02:55:36 -0700 | [diff] [blame] | 751 | TimestampGranularityMonitor tsgm = new TimestampGranularityMonitor(BlazeClock.instance()); |
ajurkowski | 280bbe2 | 2020-08-19 11:26:20 -0700 | [diff] [blame] | 752 | SkyKey actionKey1 = ActionLookupData.create(ACTION_LOOKUP_KEY, 0); |
| 753 | SkyKey actionKey2 = ActionLookupData.create(ACTION_LOOKUP_KEY, 1); |
lberki | 41cfcc7 | 2019-08-05 02:55:36 -0700 | [diff] [blame] | 754 | |
| 755 | pretendBuildTwoArtifacts(out1, actionKey1, out2, actionKey2, batchStatter, tsgm); |
| 756 | |
| 757 | // Change the file but not its size |
| 758 | FileSystemUtils.writeContentAsLatin1(out1.getPath(), "hallo"); |
| 759 | checkActionDirtiedByFile(out1, actionKey1, batchStatter, tsgm); |
| 760 | pretendBuildTwoArtifacts(out1, actionKey1, out2, actionKey2, batchStatter, tsgm); |
| 761 | |
| 762 | // Now try with a different size |
| 763 | FileSystemUtils.writeContentAsLatin1(out1.getPath(), "hallo2"); |
| 764 | checkActionDirtiedByFile(out1, actionKey1, batchStatter, tsgm); |
| 765 | } |
| 766 | |
| 767 | private void pretendBuildTwoArtifacts( |
| 768 | Artifact out1, |
| 769 | SkyKey actionKey1, |
| 770 | Artifact out2, |
| 771 | SkyKey actionKey2, |
| 772 | BatchStat batchStatter, |
| 773 | TimestampGranularityMonitor tsgm) |
| 774 | throws InterruptedException { |
| 775 | EvaluationContext evaluationContext = |
| 776 | EvaluationContext.newBuilder() |
| 777 | .setKeepGoing(false) |
| 778 | .setNumThreads(1) |
michajlo | 7a485be | 2020-07-30 11:08:46 -0700 | [diff] [blame] | 779 | .setEventHandler(NullEventHandler.INSTANCE) |
lberki | 41cfcc7 | 2019-08-05 02:55:36 -0700 | [diff] [blame] | 780 | .build(); |
| 781 | |
| 782 | tsgm.setCommandStartTime(); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 783 | differencer.inject( |
| 784 | ImmutableMap.<SkyKey, SkyValue>of( |
Janak Ramakrishnan | 5d15269 | 2017-03-20 22:07:14 +0000 | [diff] [blame] | 785 | actionKey1, |
| 786 | actionValue( |
| 787 | new TestAction( |
ulfjack | 1e1a775 | 2019-12-10 21:17:58 -0800 | [diff] [blame] | 788 | Runnables.doNothing(), |
| 789 | NestedSetBuilder.emptySet(Order.STABLE_ORDER), |
| 790 | ImmutableSet.of(out1))), |
Janak Ramakrishnan | 5d15269 | 2017-03-20 22:07:14 +0000 | [diff] [blame] | 791 | actionKey2, |
| 792 | actionValue( |
| 793 | new TestAction( |
lberki | c35878a | 2019-08-01 02:28:54 -0700 | [diff] [blame] | 794 | Runnables.doNothing(), |
ulfjack | 1e1a775 | 2019-12-10 21:17:58 -0800 | [diff] [blame] | 795 | NestedSetBuilder.emptySet(Order.STABLE_ORDER), |
lberki | c35878a | 2019-08-01 02:28:54 -0700 | [diff] [blame] | 796 | ImmutableSet.of(out2))))); |
jhorvitz | 9ad8918 | 2021-12-29 11:57:02 -0800 | [diff] [blame] | 797 | assertThat(evaluator.evaluate(ImmutableList.of(), evaluationContext).hasError()).isFalse(); |
Eric Fellheimer | e659072 | 2015-11-17 17:07:48 +0000 | [diff] [blame] | 798 | assertThat( |
ajurkowski | 64795c2 | 2022-04-01 10:38:44 -0700 | [diff] [blame] | 799 | new FilesystemValueChecker(/*tsgm=*/ null, SyscallCache.NO_CACHE, FSVC_THREADS_FOR_TEST) |
Janak Ramakrishnan | 5d15269 | 2017-03-20 22:07:14 +0000 | [diff] [blame] | 800 | .getDirtyActionValues( |
steinman | 39c00d2 | 2020-03-20 15:23:10 -0700 | [diff] [blame] | 801 | evaluator.getValues(), |
| 802 | batchStatter, |
| 803 | ModifiedFileSet.EVERYTHING_MODIFIED, |
ajurkowski | 64795c2 | 2022-04-01 10:38:44 -0700 | [diff] [blame] | 804 | /* trustRemoteArtifacts= */ false, |
| 805 | (ignored, ignored2) -> {})) |
lberki | 41cfcc7 | 2019-08-05 02:55:36 -0700 | [diff] [blame] | 806 | .isEmpty(); |
| 807 | |
| 808 | tsgm.waitForTimestampGranularity(OutErr.SYSTEM_OUT_ERR); |
| 809 | } |
| 810 | |
| 811 | private void checkActionDirtiedByFile( |
| 812 | Artifact file, SkyKey actionKey, BatchStat batchStatter, TimestampGranularityMonitor tsgm) |
| 813 | throws InterruptedException { |
Ulf Adams | c73051c6 | 2016-03-23 09:18:13 +0000 | [diff] [blame] | 814 | assertThat( |
ajurkowski | 64795c2 | 2022-04-01 10:38:44 -0700 | [diff] [blame] | 815 | new FilesystemValueChecker(tsgm, SyscallCache.NO_CACHE, FSVC_THREADS_FOR_TEST) |
lberki | 41cfcc7 | 2019-08-05 02:55:36 -0700 | [diff] [blame] | 816 | .getDirtyActionValues( |
steinman | 39c00d2 | 2020-03-20 15:23:10 -0700 | [diff] [blame] | 817 | evaluator.getValues(), |
| 818 | batchStatter, |
| 819 | ModifiedFileSet.EVERYTHING_MODIFIED, |
ajurkowski | 64795c2 | 2022-04-01 10:38:44 -0700 | [diff] [blame] | 820 | /* trustRemoteArtifacts= */ false, |
| 821 | (ignored, ignored2) -> {})) |
lberki | 41cfcc7 | 2019-08-05 02:55:36 -0700 | [diff] [blame] | 822 | .containsExactly(actionKey); |
| 823 | assertThat( |
ajurkowski | 64795c2 | 2022-04-01 10:38:44 -0700 | [diff] [blame] | 824 | new FilesystemValueChecker(tsgm, SyscallCache.NO_CACHE, FSVC_THREADS_FOR_TEST) |
Janak Ramakrishnan | 5d15269 | 2017-03-20 22:07:14 +0000 | [diff] [blame] | 825 | .getDirtyActionValues( |
| 826 | evaluator.getValues(), |
| 827 | batchStatter, |
ajurkowski | fbd4427 | 2021-12-02 16:05:49 -0800 | [diff] [blame] | 828 | ModifiedFileSet.EVERYTHING_DELETED, |
ajurkowski | 64795c2 | 2022-04-01 10:38:44 -0700 | [diff] [blame] | 829 | /* trustRemoteArtifacts= */ false, |
| 830 | (ignored, ignored2) -> {})) |
ajurkowski | fbd4427 | 2021-12-02 16:05:49 -0800 | [diff] [blame] | 831 | .containsExactly(actionKey); |
| 832 | assertThat( |
ajurkowski | 64795c2 | 2022-04-01 10:38:44 -0700 | [diff] [blame] | 833 | new FilesystemValueChecker(tsgm, SyscallCache.NO_CACHE, FSVC_THREADS_FOR_TEST) |
ajurkowski | fbd4427 | 2021-12-02 16:05:49 -0800 | [diff] [blame] | 834 | .getDirtyActionValues( |
| 835 | evaluator.getValues(), |
| 836 | batchStatter, |
steinman | 39c00d2 | 2020-03-20 15:23:10 -0700 | [diff] [blame] | 837 | new ModifiedFileSet.Builder().modify(file.getExecPath()).build(), |
ajurkowski | 64795c2 | 2022-04-01 10:38:44 -0700 | [diff] [blame] | 838 | /* trustRemoteArtifacts= */ false, |
| 839 | (ignored, ignored2) -> {})) |
lberki | 41cfcc7 | 2019-08-05 02:55:36 -0700 | [diff] [blame] | 840 | .containsExactly(actionKey); |
Ulf Adams | c73051c6 | 2016-03-23 09:18:13 +0000 | [diff] [blame] | 841 | assertThat( |
ajurkowski | 64795c2 | 2022-04-01 10:38:44 -0700 | [diff] [blame] | 842 | new FilesystemValueChecker(tsgm, SyscallCache.NO_CACHE, FSVC_THREADS_FOR_TEST) |
lberki | 41cfcc7 | 2019-08-05 02:55:36 -0700 | [diff] [blame] | 843 | .getDirtyActionValues( |
| 844 | evaluator.getValues(), |
| 845 | batchStatter, |
| 846 | new ModifiedFileSet.Builder() |
| 847 | .modify(file.getExecPath().getParentDirectory()) |
steinman | 39c00d2 | 2020-03-20 15:23:10 -0700 | [diff] [blame] | 848 | .build(), |
ajurkowski | 64795c2 | 2022-04-01 10:38:44 -0700 | [diff] [blame] | 849 | /* trustRemoteArtifacts= */ false, |
| 850 | (ignored, ignored2) -> {})) |
lberki | 41cfcc7 | 2019-08-05 02:55:36 -0700 | [diff] [blame] | 851 | .isEmpty(); |
Eric Fellheimer | e659072 | 2015-11-17 17:07:48 +0000 | [diff] [blame] | 852 | assertThat( |
ajurkowski | 64795c2 | 2022-04-01 10:38:44 -0700 | [diff] [blame] | 853 | new FilesystemValueChecker(tsgm, SyscallCache.NO_CACHE, FSVC_THREADS_FOR_TEST) |
lberki | 41cfcc7 | 2019-08-05 02:55:36 -0700 | [diff] [blame] | 854 | .getDirtyActionValues( |
steinman | 39c00d2 | 2020-03-20 15:23:10 -0700 | [diff] [blame] | 855 | evaluator.getValues(), |
| 856 | batchStatter, |
| 857 | ModifiedFileSet.NOTHING_MODIFIED, |
ajurkowski | 64795c2 | 2022-04-01 10:38:44 -0700 | [diff] [blame] | 858 | /* trustRemoteArtifacts= */ false, |
| 859 | (ignored, ignored2) -> {})) |
lberki | 41cfcc7 | 2019-08-05 02:55:36 -0700 | [diff] [blame] | 860 | .isEmpty(); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 861 | } |
| 862 | |
ajurkowski | fbd4427 | 2021-12-02 16:05:49 -0800 | [diff] [blame] | 863 | enum ModifiedSetReporting { |
| 864 | EVERYTHING_MODIFIED { |
| 865 | @Override |
| 866 | ModifiedFileSet getModifiedFileSet(PathFragment path) { |
| 867 | return ModifiedFileSet.EVERYTHING_MODIFIED; |
| 868 | } |
| 869 | }, |
| 870 | EVERYTHING_DELETED { |
| 871 | @Override |
| 872 | ModifiedFileSet getModifiedFileSet(PathFragment path) { |
| 873 | return ModifiedFileSet.EVERYTHING_DELETED; |
| 874 | } |
| 875 | }, |
| 876 | SINGLE_PATH { |
| 877 | @Override |
| 878 | ModifiedFileSet getModifiedFileSet(PathFragment path) { |
| 879 | return ModifiedFileSet.builder().modify(path).build(); |
| 880 | } |
| 881 | }; |
| 882 | |
| 883 | abstract ModifiedFileSet getModifiedFileSet(PathFragment path); |
| 884 | } |
| 885 | |
ajurkowski | bdc60b0 | 2021-11-24 08:47:05 -0800 | [diff] [blame] | 886 | @Test |
| 887 | public void getDirtyActionValues_touchedTreeDirectory_returnsEmptyDiff( |
ajurkowski | bdc60b0 | 2021-11-24 08:47:05 -0800 | [diff] [blame] | 888 | @TestParameter({"", "subdir"}) String touchedTreePath, |
ajurkowski | fbd4427 | 2021-12-02 16:05:49 -0800 | [diff] [blame] | 889 | @TestParameter ModifiedSetReporting modifiedSet) |
ajurkowski | bdc60b0 | 2021-11-24 08:47:05 -0800 | [diff] [blame] | 890 | throws Exception { |
| 891 | SpecialArtifact tree = createTreeArtifact("tree"); |
| 892 | TreeFileArtifact treeFile = TreeFileArtifact.createTreeOutput(tree, "subdir/file"); |
| 893 | FileSystemUtils.writeIsoLatin1(treeFile.getPath(), "text"); |
| 894 | SkyKey actionKey = ActionLookupData.create(ACTION_LOOKUP_KEY, 0); |
| 895 | differencer.inject( |
| 896 | ImmutableMap.of(actionKey, actionValueWithTreeArtifacts(ImmutableList.of(treeFile)))); |
| 897 | evaluate(); |
ajurkowski | bdc60b0 | 2021-11-24 08:47:05 -0800 | [diff] [blame] | 898 | FileSystemUtils.touchFile(tree.getPath().getRelative(touchedTreePath)); |
ajurkowski | 64795c2 | 2022-04-01 10:38:44 -0700 | [diff] [blame] | 899 | |
| 900 | Collection<SkyKey> dirtyActionKeys = |
| 901 | new FilesystemValueChecker(/*tsgm=*/ null, SyscallCache.NO_CACHE, FSVC_THREADS_FOR_TEST) |
| 902 | .getDirtyActionValues( |
| 903 | evaluator.getValues(), |
| 904 | batchStat.getBatchStat(fs), |
| 905 | modifiedSet.getModifiedFileSet(tree.getExecPath()), |
| 906 | /*trustRemoteArtifacts=*/ false, |
| 907 | mockModifiedOutputsReceiver); |
| 908 | |
| 909 | assertThat(dirtyActionKeys).isEmpty(); |
| 910 | assertThat(modifiedOutputsCaptor.getAllValues()).isEmpty(); |
ajurkowski | bdc60b0 | 2021-11-24 08:47:05 -0800 | [diff] [blame] | 911 | } |
Michael Thvedt | e4a7b079 | 2016-02-09 12:15:53 +0000 | [diff] [blame] | 912 | |
ajurkowski | bdc60b0 | 2021-11-24 08:47:05 -0800 | [diff] [blame] | 913 | @Test |
| 914 | public void getDirtyActionValues_deleteEmptyTreeDirectory_returnsTreeKey( |
Googler | fe72dbb | 2021-12-29 07:12:53 -0800 | [diff] [blame] | 915 | @TestParameter ModifiedSetReporting modifiedSet) throws Exception { |
ajurkowski | bdc60b0 | 2021-11-24 08:47:05 -0800 | [diff] [blame] | 916 | SpecialArtifact tree = createTreeArtifact("tree"); |
| 917 | tree.getPath().createDirectoryAndParents(); |
| 918 | SkyKey actionKey = ActionLookupData.create(ACTION_LOOKUP_KEY, 0); |
| 919 | differencer.inject( |
| 920 | ImmutableMap.of(actionKey, actionValueWithTreeArtifact(tree, TreeArtifactValue.empty()))); |
| 921 | evaluate(); |
ajurkowski | bdc60b0 | 2021-11-24 08:47:05 -0800 | [diff] [blame] | 922 | assertThat(tree.getPath().delete()).isTrue(); |
ajurkowski | 64795c2 | 2022-04-01 10:38:44 -0700 | [diff] [blame] | 923 | |
| 924 | Collection<SkyKey> dirtyActionKeys = |
| 925 | new FilesystemValueChecker(/*tsgm=*/ null, SyscallCache.NO_CACHE, FSVC_THREADS_FOR_TEST) |
| 926 | .getDirtyActionValues( |
| 927 | evaluator.getValues(), |
| 928 | batchStat.getBatchStat(fs), |
| 929 | modifiedSet.getModifiedFileSet(tree.getExecPath()), |
| 930 | /*trustRemoteArtifacts=*/ false, |
| 931 | mockModifiedOutputsReceiver); |
| 932 | |
| 933 | assertThat(dirtyActionKeys).containsExactly(actionKey); |
| 934 | assertThat(modifiedOutputsCaptor.getAllValues()).containsExactly(tree); |
ajurkowski | bdc60b0 | 2021-11-24 08:47:05 -0800 | [diff] [blame] | 935 | } |
Michael Thvedt | e4a7b079 | 2016-02-09 12:15:53 +0000 | [diff] [blame] | 936 | |
ajurkowski | bdc60b0 | 2021-11-24 08:47:05 -0800 | [diff] [blame] | 937 | @Test |
Googler | fe72dbb | 2021-12-29 07:12:53 -0800 | [diff] [blame] | 938 | public void getDirtyActionValues_treeDirectoryReplacedWithSymlink_returnsTreeKey() |
| 939 | throws Exception { |
ajurkowski | bdc60b0 | 2021-11-24 08:47:05 -0800 | [diff] [blame] | 940 | SpecialArtifact tree = createTreeArtifact("tree"); |
| 941 | tree.getPath().createDirectoryAndParents(); |
| 942 | SkyKey actionKey = ActionLookupData.create(ACTION_LOOKUP_KEY, 0); |
| 943 | differencer.inject( |
| 944 | ImmutableMap.of(actionKey, actionValueWithTreeArtifact(tree, TreeArtifactValue.empty()))); |
| 945 | evaluate(); |
ajurkowski | bdc60b0 | 2021-11-24 08:47:05 -0800 | [diff] [blame] | 946 | Path dummyEmptyDir = fs.getPath("/bin").getRelative("dir"); |
| 947 | dummyEmptyDir.createDirectoryAndParents(); |
| 948 | assertThat(tree.getPath().delete()).isTrue(); |
| 949 | tree.getPath().createSymbolicLink(dummyEmptyDir); |
Rumou Duan | 45e8e57 | 2016-06-17 16:43:44 +0000 | [diff] [blame] | 950 | |
ajurkowski | 64795c2 | 2022-04-01 10:38:44 -0700 | [diff] [blame] | 951 | Collection<SkyKey> dirtyActionKeys = |
| 952 | new FilesystemValueChecker(/*tsgm=*/ null, SyscallCache.NO_CACHE, FSVC_THREADS_FOR_TEST) |
| 953 | .getDirtyActionValues( |
| 954 | evaluator.getValues(), |
| 955 | batchStat.getBatchStat(fs), |
| 956 | ModifiedFileSet.EVERYTHING_MODIFIED, |
| 957 | /* trustRemoteArtifacts= */ false, |
| 958 | mockModifiedOutputsReceiver); |
| 959 | |
| 960 | assertThat(dirtyActionKeys).containsExactly(actionKey); |
| 961 | assertThat(modifiedOutputsCaptor.getAllValues()).containsExactly(tree); |
ajurkowski | bdc60b0 | 2021-11-24 08:47:05 -0800 | [diff] [blame] | 962 | } |
| 963 | |
| 964 | @Test |
| 965 | public void getDirtyActionValues_modifiedTreeFile_returnsTreeKey( |
Googler | fe72dbb | 2021-12-29 07:12:53 -0800 | [diff] [blame] | 966 | @TestParameter ModifiedSetReporting modifiedSet) throws Exception { |
ajurkowski | bdc60b0 | 2021-11-24 08:47:05 -0800 | [diff] [blame] | 967 | SpecialArtifact tree = createTreeArtifact("tree"); |
| 968 | TreeFileArtifact treeFile = TreeFileArtifact.createTreeOutput(tree, "file"); |
| 969 | FileSystemUtils.writeIsoLatin1(treeFile.getPath(), "text"); |
| 970 | SkyKey actionKey = ActionLookupData.create(ACTION_LOOKUP_KEY, 0); |
| 971 | differencer.inject( |
| 972 | ImmutableMap.of(actionKey, actionValueWithTreeArtifacts(ImmutableList.of(treeFile)))); |
| 973 | evaluate(); |
ajurkowski | bdc60b0 | 2021-11-24 08:47:05 -0800 | [diff] [blame] | 974 | FileSystemUtils.writeIsoLatin1(treeFile.getPath(), "other text"); |
ajurkowski | 64795c2 | 2022-04-01 10:38:44 -0700 | [diff] [blame] | 975 | |
| 976 | Collection<SkyKey> dirtyActionKeys = |
| 977 | new FilesystemValueChecker(/*tsgm=*/ null, SyscallCache.NO_CACHE, FSVC_THREADS_FOR_TEST) |
| 978 | .getDirtyActionValues( |
| 979 | evaluator.getValues(), |
| 980 | batchStat.getBatchStat(fs), |
| 981 | modifiedSet.getModifiedFileSet(treeFile.getExecPath()), |
| 982 | /*trustRemoteArtifacts=*/ false, |
| 983 | mockModifiedOutputsReceiver); |
| 984 | |
| 985 | assertThat(dirtyActionKeys).containsExactly(actionKey); |
| 986 | assertThat(modifiedOutputsCaptor.getAllValues()).containsExactly(treeFile); |
ajurkowski | bdc60b0 | 2021-11-24 08:47:05 -0800 | [diff] [blame] | 987 | } |
| 988 | |
| 989 | @Test |
| 990 | public void getDirtyActionValues_addedTreeFile_returnsTreeKey( |
Googler | fe72dbb | 2021-12-29 07:12:53 -0800 | [diff] [blame] | 991 | @TestParameter ModifiedSetReporting modifiedSet) throws Exception { |
ajurkowski | bdc60b0 | 2021-11-24 08:47:05 -0800 | [diff] [blame] | 992 | SpecialArtifact tree = createTreeArtifact("tree"); |
| 993 | TreeFileArtifact treeFile = TreeFileArtifact.createTreeOutput(tree, "file1"); |
| 994 | FileSystemUtils.writeIsoLatin1(treeFile.getPath()); |
| 995 | SkyKey actionKey = ActionLookupData.create(ACTION_LOOKUP_KEY, 0); |
| 996 | differencer.inject( |
| 997 | ImmutableMap.of(actionKey, actionValueWithTreeArtifacts(ImmutableList.of(treeFile)))); |
| 998 | evaluate(); |
| 999 | |
| 1000 | TreeFileArtifact newFile = TreeFileArtifact.createTreeOutput(tree, "file2"); |
| 1001 | FileSystemUtils.writeIsoLatin1(newFile.getPath()); |
ajurkowski | 64795c2 | 2022-04-01 10:38:44 -0700 | [diff] [blame] | 1002 | Collection<SkyKey> dirtyActionValues = |
| 1003 | new FilesystemValueChecker(/*tsgm=*/ null, SyscallCache.NO_CACHE, FSVC_THREADS_FOR_TEST) |
| 1004 | .getDirtyActionValues( |
| 1005 | evaluator.getValues(), |
| 1006 | batchStat.getBatchStat(fs), |
| 1007 | modifiedSet.getModifiedFileSet(newFile.getExecPath()), |
| 1008 | /*trustRemoteArtifacts=*/ false, |
| 1009 | mockModifiedOutputsReceiver); |
| 1010 | |
| 1011 | assertThat(dirtyActionValues).containsExactly(actionKey); |
| 1012 | assertThat(modifiedOutputsCaptor.getAllValues()).containsExactly(tree); |
ajurkowski | bdc60b0 | 2021-11-24 08:47:05 -0800 | [diff] [blame] | 1013 | } |
| 1014 | |
| 1015 | @Test |
| 1016 | public void getDirtyActionValues_addedTreeFileToEmptyTree_returnsTreeKey( |
Googler | fe72dbb | 2021-12-29 07:12:53 -0800 | [diff] [blame] | 1017 | @TestParameter ModifiedSetReporting modifiedSet) throws Exception { |
ajurkowski | bdc60b0 | 2021-11-24 08:47:05 -0800 | [diff] [blame] | 1018 | SpecialArtifact tree = createTreeArtifact("tree"); |
| 1019 | tree.getPath().createDirectoryAndParents(); |
| 1020 | SkyKey actionKey = ActionLookupData.create(ACTION_LOOKUP_KEY, 0); |
| 1021 | differencer.inject( |
| 1022 | ImmutableMap.of(actionKey, actionValueWithTreeArtifact(tree, TreeArtifactValue.empty()))); |
| 1023 | evaluate(); |
ajurkowski | bdc60b0 | 2021-11-24 08:47:05 -0800 | [diff] [blame] | 1024 | TreeFileArtifact newFile = TreeFileArtifact.createTreeOutput(tree, "file"); |
| 1025 | FileSystemUtils.writeIsoLatin1(newFile.getPath()); |
ajurkowski | 64795c2 | 2022-04-01 10:38:44 -0700 | [diff] [blame] | 1026 | |
| 1027 | Collection<SkyKey> dirtyActionKeys = |
| 1028 | new FilesystemValueChecker(/*tsgm=*/ null, SyscallCache.NO_CACHE, FSVC_THREADS_FOR_TEST) |
| 1029 | .getDirtyActionValues( |
| 1030 | evaluator.getValues(), |
| 1031 | batchStat.getBatchStat(fs), |
| 1032 | modifiedSet.getModifiedFileSet(newFile.getExecPath()), |
| 1033 | /*trustRemoteArtifacts=*/ false, |
| 1034 | mockModifiedOutputsReceiver); |
| 1035 | |
| 1036 | assertThat(dirtyActionKeys).containsExactly(actionKey); |
| 1037 | assertThat(modifiedOutputsCaptor.getAllValues()).containsExactly(tree); |
ajurkowski | bdc60b0 | 2021-11-24 08:47:05 -0800 | [diff] [blame] | 1038 | } |
| 1039 | |
| 1040 | @Test |
| 1041 | public void getDirtyActionValues_deletedTreeFile_returnsTreeKey( |
Googler | fe72dbb | 2021-12-29 07:12:53 -0800 | [diff] [blame] | 1042 | @TestParameter ModifiedSetReporting modifiedSet) throws Exception { |
ajurkowski | bdc60b0 | 2021-11-24 08:47:05 -0800 | [diff] [blame] | 1043 | SpecialArtifact tree = createTreeArtifact("tree"); |
| 1044 | TreeFileArtifact treeFile = TreeFileArtifact.createTreeOutput(tree, "file"); |
| 1045 | FileSystemUtils.writeIsoLatin1(treeFile.getPath()); |
| 1046 | SkyKey actionKey = ActionLookupData.create(ACTION_LOOKUP_KEY, 0); |
| 1047 | differencer.inject( |
| 1048 | ImmutableMap.of(actionKey, actionValueWithTreeArtifacts(ImmutableList.of(treeFile)))); |
| 1049 | evaluate(); |
ajurkowski | bdc60b0 | 2021-11-24 08:47:05 -0800 | [diff] [blame] | 1050 | assertThat(treeFile.getPath().delete()).isTrue(); |
ajurkowski | 64795c2 | 2022-04-01 10:38:44 -0700 | [diff] [blame] | 1051 | |
| 1052 | Collection<SkyKey> dirtyActionKeys = |
| 1053 | new FilesystemValueChecker(/*tsgm=*/ null, SyscallCache.NO_CACHE, FSVC_THREADS_FOR_TEST) |
| 1054 | .getDirtyActionValues( |
| 1055 | evaluator.getValues(), |
| 1056 | batchStat.getBatchStat(fs), |
| 1057 | modifiedSet.getModifiedFileSet(treeFile.getExecPath()), |
| 1058 | /*trustRemoteArtifacts=*/ false, |
| 1059 | mockModifiedOutputsReceiver); |
| 1060 | |
| 1061 | assertThat(dirtyActionKeys).containsExactly(actionKey); |
| 1062 | assertThat(modifiedOutputsCaptor.getAllValues()).containsExactly(treeFile, tree); |
ajurkowski | bdc60b0 | 2021-11-24 08:47:05 -0800 | [diff] [blame] | 1063 | } |
| 1064 | |
| 1065 | @Test |
Googler | fe72dbb | 2021-12-29 07:12:53 -0800 | [diff] [blame] | 1066 | public void getDirtyActionValues_everythingModified_returnsAllKeys() throws Exception { |
ajurkowski | bdc60b0 | 2021-11-24 08:47:05 -0800 | [diff] [blame] | 1067 | SpecialArtifact tree1 = createTreeArtifact("tree1"); |
| 1068 | TreeFileArtifact tree1File = TreeFileArtifact.createTreeOutput(tree1, "file"); |
| 1069 | FileSystemUtils.writeIsoLatin1(tree1File.getPath(), "text"); |
| 1070 | SpecialArtifact tree2 = createTreeArtifact("tree2"); |
| 1071 | TreeFileArtifact tree2File = TreeFileArtifact.createTreeOutput(tree2, "file"); |
| 1072 | FileSystemUtils.writeIsoLatin1(tree2File.getPath()); |
ajurkowski | 280bbe2 | 2020-08-19 11:26:20 -0700 | [diff] [blame] | 1073 | SkyKey actionKey1 = ActionLookupData.create(ACTION_LOOKUP_KEY, 0); |
| 1074 | SkyKey actionKey2 = ActionLookupData.create(ACTION_LOOKUP_KEY, 1); |
Michael Thvedt | e4a7b079 | 2016-02-09 12:15:53 +0000 | [diff] [blame] | 1075 | differencer.inject( |
jhorvitz | 3daedc3 | 2020-07-22 18:33:55 -0700 | [diff] [blame] | 1076 | ImmutableMap.of( |
Janak Ramakrishnan | 5d15269 | 2017-03-20 22:07:14 +0000 | [diff] [blame] | 1077 | actionKey1, |
ajurkowski | bdc60b0 | 2021-11-24 08:47:05 -0800 | [diff] [blame] | 1078 | actionValueWithTreeArtifacts(ImmutableList.of(tree1File)), |
Janak Ramakrishnan | 5d15269 | 2017-03-20 22:07:14 +0000 | [diff] [blame] | 1079 | actionKey2, |
ajurkowski | 64795c2 | 2022-04-01 10:38:44 -0700 | [diff] [blame] | 1080 | actionValueWithTreeArtifacts(ImmutableList.of(tree2File)))); |
ajurkowski | bdc60b0 | 2021-11-24 08:47:05 -0800 | [diff] [blame] | 1081 | evaluate(); |
ajurkowski | bdc60b0 | 2021-11-24 08:47:05 -0800 | [diff] [blame] | 1082 | FileSystemUtils.writeIsoLatin1(tree1File.getPath(), "new text"); |
| 1083 | assertThat(tree2File.getPath().delete()).isTrue(); |
ajurkowski | 64795c2 | 2022-04-01 10:38:44 -0700 | [diff] [blame] | 1084 | |
| 1085 | Collection<SkyKey> dirtyActionKeys = |
| 1086 | new FilesystemValueChecker(/*tsgm=*/ null, SyscallCache.NO_CACHE, FSVC_THREADS_FOR_TEST) |
| 1087 | .getDirtyActionValues( |
| 1088 | evaluator.getValues(), |
| 1089 | batchStat.getBatchStat(fs), |
| 1090 | ModifiedFileSet.EVERYTHING_MODIFIED, |
| 1091 | /*trustRemoteArtifacts=*/ false, |
| 1092 | mockModifiedOutputsReceiver); |
| 1093 | |
| 1094 | assertThat(dirtyActionKeys).containsExactly(actionKey1, actionKey2); |
| 1095 | assertThat(modifiedOutputsCaptor.getAllValues()).containsExactly(tree1File, tree2, tree2File); |
ajurkowski | bdc60b0 | 2021-11-24 08:47:05 -0800 | [diff] [blame] | 1096 | } |
| 1097 | |
| 1098 | @Test |
| 1099 | public void getDirtyActionValues_changedFileNotInModifiedSet_returnsKeysFromSetOnly( |
Googler | fe72dbb | 2021-12-29 07:12:53 -0800 | [diff] [blame] | 1100 | @TestParameter boolean reportFirst) throws Exception { |
ajurkowski | bdc60b0 | 2021-11-24 08:47:05 -0800 | [diff] [blame] | 1101 | SpecialArtifact tree1 = createTreeArtifact("tree1"); |
| 1102 | TreeFileArtifact tree1File = TreeFileArtifact.createTreeOutput(tree1, "file"); |
| 1103 | FileSystemUtils.writeIsoLatin1(tree1File.getPath(), "text"); |
| 1104 | SpecialArtifact tree2 = createTreeArtifact("tree2"); |
| 1105 | TreeFileArtifact tree2File = TreeFileArtifact.createTreeOutput(tree2, "file"); |
| 1106 | FileSystemUtils.writeIsoLatin1(tree2File.getPath()); |
| 1107 | SkyKey actionKey1 = ActionLookupData.create(ACTION_LOOKUP_KEY, 0); |
| 1108 | SkyKey actionKey2 = ActionLookupData.create(ACTION_LOOKUP_KEY, 1); |
| 1109 | differencer.inject( |
| 1110 | ImmutableMap.of( |
| 1111 | actionKey1, |
| 1112 | actionValueWithTreeArtifacts(ImmutableList.of(tree1File)), |
| 1113 | actionKey2, |
| 1114 | actionValueWithTreeArtifacts(ImmutableList.of(tree2File)))); |
| 1115 | evaluate(); |
ajurkowski | bdc60b0 | 2021-11-24 08:47:05 -0800 | [diff] [blame] | 1116 | FileSystemUtils.writeIsoLatin1(tree1File.getPath(), "new text"); |
| 1117 | assertThat(tree2File.getPath().delete()).isTrue(); |
ajurkowski | 64795c2 | 2022-04-01 10:38:44 -0700 | [diff] [blame] | 1118 | |
| 1119 | Collection<SkyKey> dirtyActionKeys = |
| 1120 | new FilesystemValueChecker(/*tsgm=*/ null, SyscallCache.NO_CACHE, FSVC_THREADS_FOR_TEST) |
| 1121 | .getDirtyActionValues( |
| 1122 | evaluator.getValues(), |
| 1123 | batchStat.getBatchStat(fs), |
| 1124 | ModifiedFileSet.builder() |
| 1125 | .modify((reportFirst ? tree1File : tree2File).getExecPath()) |
| 1126 | .build(), |
| 1127 | /*trustRemoteArtifacts=*/ false, |
| 1128 | mockModifiedOutputsReceiver); |
| 1129 | |
| 1130 | assertThat(dirtyActionKeys).containsExactly(reportFirst ? actionKey1 : actionKey2); |
| 1131 | assertThat(modifiedOutputsCaptor.getAllValues()) |
| 1132 | .containsExactlyElementsIn( |
| 1133 | reportFirst ? ImmutableList.of(tree1File) : ImmutableList.of(tree2File, tree2)); |
ajurkowski | bdc60b0 | 2021-11-24 08:47:05 -0800 | [diff] [blame] | 1134 | } |
| 1135 | |
| 1136 | @Test |
Googler | fe72dbb | 2021-12-29 07:12:53 -0800 | [diff] [blame] | 1137 | public void getDirtyActionValues_middleFileSkippedInModifiedFileSet_returnsKeysFromSetOnly() |
| 1138 | throws Exception { |
ajurkowski | bdc60b0 | 2021-11-24 08:47:05 -0800 | [diff] [blame] | 1139 | SpecialArtifact treeA = createTreeArtifact("a_tree"); |
| 1140 | TreeFileArtifact treeAFile = TreeFileArtifact.createTreeOutput(treeA, "file"); |
| 1141 | FileSystemUtils.writeIsoLatin1(treeAFile.getPath()); |
| 1142 | SpecialArtifact treeB = createTreeArtifact("b_tree"); |
| 1143 | TreeFileArtifact treeBFile = TreeFileArtifact.createTreeOutput(treeB, "file"); |
| 1144 | FileSystemUtils.writeIsoLatin1(treeBFile.getPath()); |
| 1145 | SpecialArtifact treeC = createTreeArtifact("c_tree"); |
| 1146 | TreeFileArtifact treeCFile = TreeFileArtifact.createTreeOutput(treeC, "file"); |
| 1147 | FileSystemUtils.writeIsoLatin1(treeCFile.getPath()); |
| 1148 | SkyKey actionKey1 = ActionLookupData.create(ACTION_LOOKUP_KEY, 0); |
| 1149 | SkyKey actionKey2 = ActionLookupData.create(ACTION_LOOKUP_KEY, 1); |
| 1150 | SkyKey actionKey3 = ActionLookupData.create(ACTION_LOOKUP_KEY, 2); |
| 1151 | differencer.inject( |
| 1152 | ImmutableMap.of( |
| 1153 | actionKey1, |
| 1154 | actionValueWithTreeArtifacts(ImmutableList.of(treeAFile)), |
| 1155 | actionKey2, |
| 1156 | actionValueWithTreeArtifacts(ImmutableList.of(treeBFile)), |
| 1157 | actionKey3, |
| 1158 | actionValueWithTreeArtifacts(ImmutableList.of(treeCFile)))); |
| 1159 | evaluate(); |
ajurkowski | bdc60b0 | 2021-11-24 08:47:05 -0800 | [diff] [blame] | 1160 | assertThat(treeAFile.getPath().delete()).isTrue(); |
| 1161 | assertThat(treeBFile.getPath().delete()).isTrue(); |
| 1162 | assertThat(treeCFile.getPath().delete()).isTrue(); |
ajurkowski | 64795c2 | 2022-04-01 10:38:44 -0700 | [diff] [blame] | 1163 | |
| 1164 | Collection<SkyKey> dirtyActionKeys = |
| 1165 | new FilesystemValueChecker(/*tsgm=*/ null, SyscallCache.NO_CACHE, FSVC_THREADS_FOR_TEST) |
| 1166 | .getDirtyActionValues( |
| 1167 | evaluator.getValues(), |
| 1168 | batchStat.getBatchStat(fs), |
| 1169 | ModifiedFileSet.builder() |
| 1170 | .modify(treeAFile.getExecPath()) |
| 1171 | .modify(treeCFile.getExecPath()) |
| 1172 | .build(), |
| 1173 | /*trustRemoteArtifacts=*/ false, |
| 1174 | mockModifiedOutputsReceiver); |
| 1175 | |
| 1176 | assertThat(dirtyActionKeys).containsExactly(actionKey1, actionKey3); |
| 1177 | assertThat(modifiedOutputsCaptor.getAllValues()) |
| 1178 | .containsExactly(treeAFile, treeA, treeCFile, treeC); |
ajurkowski | bdc60b0 | 2021-11-24 08:47:05 -0800 | [diff] [blame] | 1179 | } |
| 1180 | |
| 1181 | @Test |
Googler | fe72dbb | 2021-12-29 07:12:53 -0800 | [diff] [blame] | 1182 | public void getDirtyActionValues_nothingModified_returnsEmptyDiff() throws Exception { |
ajurkowski | bdc60b0 | 2021-11-24 08:47:05 -0800 | [diff] [blame] | 1183 | SpecialArtifact tree = createTreeArtifact("tree"); |
| 1184 | TreeFileArtifact treeFile = TreeFileArtifact.createTreeOutput(tree, "file"); |
| 1185 | FileSystemUtils.writeIsoLatin1(treeFile.getPath()); |
| 1186 | SkyKey actionKey = ActionLookupData.create(ACTION_LOOKUP_KEY, 0); |
| 1187 | differencer.inject( |
| 1188 | ImmutableMap.of(actionKey, actionValueWithTreeArtifacts(ImmutableList.of(treeFile)))); |
| 1189 | evaluate(); |
ajurkowski | bdc60b0 | 2021-11-24 08:47:05 -0800 | [diff] [blame] | 1190 | assertThat(treeFile.getPath().delete()).isTrue(); |
ajurkowski | 64795c2 | 2022-04-01 10:38:44 -0700 | [diff] [blame] | 1191 | |
| 1192 | Collection<SkyKey> dirtyActionKeys = |
| 1193 | new FilesystemValueChecker(/*tsgm=*/ null, SyscallCache.NO_CACHE, FSVC_THREADS_FOR_TEST) |
| 1194 | .getDirtyActionValues( |
| 1195 | evaluator.getValues(), |
| 1196 | batchStat.getBatchStat(fs), |
| 1197 | ModifiedFileSet.NOTHING_MODIFIED, |
| 1198 | /*trustRemoteArtifacts=*/ false, |
| 1199 | mockModifiedOutputsReceiver); |
| 1200 | |
| 1201 | assertThat(dirtyActionKeys).isEmpty(); |
| 1202 | assertThat(modifiedOutputsCaptor.getAllValues()).isEmpty(); |
ajurkowski | bdc60b0 | 2021-11-24 08:47:05 -0800 | [diff] [blame] | 1203 | } |
| 1204 | |
| 1205 | private void evaluate() throws InterruptedException { |
Googler | 1002867 | 2018-10-25 12:14:34 -0700 | [diff] [blame] | 1206 | EvaluationContext evaluationContext = |
| 1207 | EvaluationContext.newBuilder() |
| 1208 | .setKeepGoing(false) |
| 1209 | .setNumThreads(1) |
michajlo | 7a485be | 2020-07-30 11:08:46 -0700 | [diff] [blame] | 1210 | .setEventHandler(NullEventHandler.INSTANCE) |
Googler | 1002867 | 2018-10-25 12:14:34 -0700 | [diff] [blame] | 1211 | .build(); |
jhorvitz | 9ad8918 | 2021-12-29 11:57:02 -0800 | [diff] [blame] | 1212 | assertThat(evaluator.evaluate(ImmutableList.of(), evaluationContext).hasError()).isFalse(); |
Michael Thvedt | e4a7b079 | 2016-02-09 12:15:53 +0000 | [diff] [blame] | 1213 | } |
| 1214 | |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 1215 | private Artifact createDerivedArtifact(String relPath) throws IOException { |
janakr | 448f1cf | 2020-03-30 09:12:44 -0700 | [diff] [blame] | 1216 | String outSegment = "bin"; |
| 1217 | Path outputPath = fs.getPath("/" + outSegment); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 1218 | outputPath.createDirectory(); |
janakr | aea0560 | 2019-05-22 15:41:29 -0700 | [diff] [blame] | 1219 | return ActionsTestUtil.createArtifact( |
Googler | f0b0c39 | 2021-01-27 17:56:52 -0800 | [diff] [blame] | 1220 | ArtifactRoot.asDerivedRoot(fs.getPath("/"), RootType.Output, outSegment), |
Googler | 147c287 | 2021-01-21 08:47:48 -0800 | [diff] [blame] | 1221 | outputPath.getRelative(relPath)); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 1222 | } |
| 1223 | |
Han-Wen Nienhuys | 3b2eae3 | 2015-10-28 16:35:08 +0000 | [diff] [blame] | 1224 | @Test |
steinman | 7184cb3 | 2020-04-20 14:33:03 -0700 | [diff] [blame] | 1225 | // TODO(b/154337187): Remove the following annotation to re-enable once this test is de-flaked. |
| 1226 | @Ignore |
| 1227 | public void testDirtyActions() throws Exception { |
jhorvitz | 3daedc3 | 2020-07-22 18:33:55 -0700 | [diff] [blame] | 1228 | checkDirtyActions(null); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 1229 | } |
| 1230 | |
Han-Wen Nienhuys | 3b2eae3 | 2015-10-28 16:35:08 +0000 | [diff] [blame] | 1231 | @Test |
steinman | 7184cb3 | 2020-04-20 14:33:03 -0700 | [diff] [blame] | 1232 | // TODO(b/154337187): Remove the following annotation to re-enable once this test is de-flaked. |
| 1233 | @Ignore |
| 1234 | public void testDirtyActionsBatchStat() throws Exception { |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 1235 | checkDirtyActions( |
| 1236 | new BatchStat() { |
| 1237 | @Override |
| 1238 | public List<FileStatusWithDigest> batchStat( |
| 1239 | boolean useDigest, boolean includeLinks, Iterable<PathFragment> paths) |
| 1240 | throws IOException { |
| 1241 | List<FileStatusWithDigest> stats = new ArrayList<>(); |
| 1242 | for (PathFragment pathFrag : paths) { |
| 1243 | stats.add( |
janakr | 61b3bb2 | 2022-03-07 10:11:00 -0800 | [diff] [blame] | 1244 | FileStatusWithDigestAdapter.maybeAdapt( |
tomlu | 6c91906 | 2018-01-11 17:32:09 -0800 | [diff] [blame] | 1245 | fs.getPath("/").getRelative(pathFrag).statIfFound(Symlinks.NOFOLLOW))); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 1246 | } |
| 1247 | return stats; |
| 1248 | } |
jhorvitz | 3daedc3 | 2020-07-22 18:33:55 -0700 | [diff] [blame] | 1249 | }); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 1250 | } |
| 1251 | |
Han-Wen Nienhuys | 3b2eae3 | 2015-10-28 16:35:08 +0000 | [diff] [blame] | 1252 | @Test |
steinman | 68e78f5 | 2020-04-23 12:30:19 -0700 | [diff] [blame] | 1253 | // TODO(b/154337187): Remove the following annotation to re-enable once this test is de-flaked. |
| 1254 | @Ignore |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 1255 | public void testDirtyActionsBatchStatWithDigest() throws Exception { |
| 1256 | checkDirtyActions( |
| 1257 | new BatchStat() { |
| 1258 | @Override |
| 1259 | public List<FileStatusWithDigest> batchStat( |
| 1260 | boolean useDigest, boolean includeLinks, Iterable<PathFragment> paths) |
| 1261 | throws IOException { |
| 1262 | List<FileStatusWithDigest> stats = new ArrayList<>(); |
| 1263 | for (PathFragment pathFrag : paths) { |
tomlu | 6c91906 | 2018-01-11 17:32:09 -0800 | [diff] [blame] | 1264 | final Path path = fs.getPath("/").getRelative(pathFrag); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 1265 | stats.add(statWithDigest(path, path.statIfFound(Symlinks.NOFOLLOW))); |
| 1266 | } |
| 1267 | return stats; |
| 1268 | } |
jhorvitz | 3daedc3 | 2020-07-22 18:33:55 -0700 | [diff] [blame] | 1269 | }); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 1270 | } |
| 1271 | |
Han-Wen Nienhuys | 3b2eae3 | 2015-10-28 16:35:08 +0000 | [diff] [blame] | 1272 | @Test |
steinman | 68e78f5 | 2020-04-23 12:30:19 -0700 | [diff] [blame] | 1273 | // TODO(b/154337187): Remove the following annotation to re-enable once this test is de-flaked. |
| 1274 | @Ignore |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 1275 | public void testDirtyActionsBatchStatFallback() throws Exception { |
| 1276 | checkDirtyActions( |
| 1277 | new BatchStat() { |
| 1278 | @Override |
| 1279 | public List<FileStatusWithDigest> batchStat( |
| 1280 | boolean useDigest, boolean includeLinks, Iterable<PathFragment> paths) |
| 1281 | throws IOException { |
| 1282 | throw new IOException("try again"); |
| 1283 | } |
jhorvitz | 3daedc3 | 2020-07-22 18:33:55 -0700 | [diff] [blame] | 1284 | }); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 1285 | } |
| 1286 | |
Michael Thvedt | e4a7b079 | 2016-02-09 12:15:53 +0000 | [diff] [blame] | 1287 | // TODO(bazel-team): Add some tests for FileSystemValueChecker#changedKeys*() methods. |
| 1288 | // Presently these appear to be untested. |
| 1289 | |
ajurkowski | e298291 | 2020-04-09 10:32:08 -0700 | [diff] [blame] | 1290 | private static ActionExecutionValue actionValue(Action action) { |
lberki | f7eee1e | 2019-07-31 05:49:10 -0700 | [diff] [blame] | 1291 | Map<Artifact, FileArtifactValue> artifactData = new HashMap<>(); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 1292 | for (Artifact output : action.getOutputs()) { |
| 1293 | try { |
| 1294 | Path path = output.getPath(); |
lberki | c35878a | 2019-08-01 02:28:54 -0700 | [diff] [blame] | 1295 | FileArtifactValue noDigest = |
| 1296 | ActionMetadataHandler.fileArtifactValueFromArtifact( |
| 1297 | output, |
janakr | 61b3bb2 | 2022-03-07 10:11:00 -0800 | [diff] [blame] | 1298 | FileStatusWithDigestAdapter.maybeAdapt(path.statIfFound(Symlinks.NOFOLLOW)), |
janakr | 95278b4 | 2022-03-04 13:41:46 -0800 | [diff] [blame] | 1299 | SyscallCache.NO_CACHE, |
lberki | c35878a | 2019-08-01 02:28:54 -0700 | [diff] [blame] | 1300 | null); |
| 1301 | FileArtifactValue withDigest = |
jhorvitz | 7f55cb7 | 2021-12-16 18:52:24 -0800 | [diff] [blame] | 1302 | FileArtifactValue.createFromInjectedDigest(noDigest, path.getDigest()); |
lberki | c35878a | 2019-08-01 02:28:54 -0700 | [diff] [blame] | 1303 | artifactData.put(output, withDigest); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 1304 | } catch (IOException e) { |
| 1305 | throw new IllegalStateException(e); |
| 1306 | } |
| 1307 | } |
jhorvitz | 7f55cb7 | 2021-12-16 18:52:24 -0800 | [diff] [blame] | 1308 | return ActionExecutionValue.createForTesting( |
jhorvitz | 8470e43 | 2021-12-14 09:51:17 -0800 | [diff] [blame] | 1309 | ImmutableMap.copyOf(artifactData), |
Googler | 974879d | 2020-05-27 13:25:52 -0700 | [diff] [blame] | 1310 | /*treeArtifactData=*/ ImmutableMap.of(), |
jhorvitz | 7f55cb7 | 2021-12-16 18:52:24 -0800 | [diff] [blame] | 1311 | /*outputSymlinks=*/ null); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 1312 | } |
| 1313 | |
jhorvitz | ed7ec3b | 2020-07-24 15:08:03 -0700 | [diff] [blame] | 1314 | private static ActionExecutionValue actionValueWithTreeArtifact( |
| 1315 | SpecialArtifact output, TreeArtifactValue tree) { |
jhorvitz | 7f55cb7 | 2021-12-16 18:52:24 -0800 | [diff] [blame] | 1316 | return ActionExecutionValue.createForTesting( |
| 1317 | ImmutableMap.of(), ImmutableMap.of(output, tree), /*outputSymlinks=*/ null); |
buchgr | 4992ae2 | 2019-03-20 04:23:32 -0700 | [diff] [blame] | 1318 | } |
| 1319 | |
ajurkowski | e298291 | 2020-04-09 10:32:08 -0700 | [diff] [blame] | 1320 | private static ActionExecutionValue actionValueWithRemoteArtifact( |
buchgr | 4992ae2 | 2019-03-20 04:23:32 -0700 | [diff] [blame] | 1321 | Artifact output, RemoteFileArtifactValue value) { |
jhorvitz | 7f55cb7 | 2021-12-16 18:52:24 -0800 | [diff] [blame] | 1322 | return ActionExecutionValue.createForTesting( |
| 1323 | ImmutableMap.of(output, value), ImmutableMap.of(), /*outputSymlinks=*/ null); |
buchgr | 4992ae2 | 2019-03-20 04:23:32 -0700 | [diff] [blame] | 1324 | } |
| 1325 | |
| 1326 | private RemoteFileArtifactValue createRemoteFileArtifactValue(String contents) { |
| 1327 | byte[] data = contents.getBytes(); |
| 1328 | DigestHashFunction hashFn = fs.getDigestFunction(); |
| 1329 | HashCode hash = hashFn.getHashFunction().hashBytes(data); |
George Gensure | 3ef8fb9 | 2020-05-06 09:49:48 -0700 | [diff] [blame] | 1330 | return new RemoteFileArtifactValue(hash.asBytes(), data.length, -1, "action-id"); |
buchgr | 4992ae2 | 2019-03-20 04:23:32 -0700 | [diff] [blame] | 1331 | } |
| 1332 | |
| 1333 | @Test |
| 1334 | public void testRemoteAndLocalArtifacts() throws Exception { |
| 1335 | // Test that injected remote artifacts are trusted by the FileSystemValueChecker |
steinman | 39c00d2 | 2020-03-20 15:23:10 -0700 | [diff] [blame] | 1336 | // if it is configured to trust remote artifacts, and that local files always take precedence |
| 1337 | // over remote files. |
ajurkowski | 280bbe2 | 2020-08-19 11:26:20 -0700 | [diff] [blame] | 1338 | SkyKey actionKey1 = ActionLookupData.create(ACTION_LOOKUP_KEY, 0); |
| 1339 | SkyKey actionKey2 = ActionLookupData.create(ACTION_LOOKUP_KEY, 1); |
buchgr | 4992ae2 | 2019-03-20 04:23:32 -0700 | [diff] [blame] | 1340 | |
| 1341 | Artifact out1 = createDerivedArtifact("foo"); |
| 1342 | Artifact out2 = createDerivedArtifact("bar"); |
| 1343 | Map<SkyKey, SkyValue> metadataToInject = new HashMap<>(); |
| 1344 | metadataToInject.put( |
| 1345 | actionKey1, |
| 1346 | actionValueWithRemoteArtifact(out1, createRemoteFileArtifactValue("foo-content"))); |
| 1347 | metadataToInject.put( |
| 1348 | actionKey2, |
| 1349 | actionValueWithRemoteArtifact(out2, createRemoteFileArtifactValue("bar-content"))); |
| 1350 | differencer.inject(metadataToInject); |
| 1351 | |
| 1352 | EvaluationContext evaluationContext = |
| 1353 | EvaluationContext.newBuilder() |
| 1354 | .setKeepGoing(false) |
| 1355 | .setNumThreads(1) |
michajlo | 7a485be | 2020-07-30 11:08:46 -0700 | [diff] [blame] | 1356 | .setEventHandler(NullEventHandler.INSTANCE) |
buchgr | 4992ae2 | 2019-03-20 04:23:32 -0700 | [diff] [blame] | 1357 | .build(); |
| 1358 | assertThat( |
jhorvitz | 9ad8918 | 2021-12-29 11:57:02 -0800 | [diff] [blame] | 1359 | evaluator |
| 1360 | .evaluate(ImmutableList.of(actionKey1, actionKey2), evaluationContext) |
| 1361 | .hasError()) |
buchgr | 4992ae2 | 2019-03-20 04:23:32 -0700 | [diff] [blame] | 1362 | .isFalse(); |
| 1363 | assertThat( |
ajurkowski | 64795c2 | 2022-04-01 10:38:44 -0700 | [diff] [blame] | 1364 | new FilesystemValueChecker(/*tsgm=*/ null, SyscallCache.NO_CACHE, FSVC_THREADS_FOR_TEST) |
buchgr | 4992ae2 | 2019-03-20 04:23:32 -0700 | [diff] [blame] | 1365 | .getDirtyActionValues( |
| 1366 | evaluator.getValues(), |
| 1367 | /* batchStatter= */ null, |
steinman | 39c00d2 | 2020-03-20 15:23:10 -0700 | [diff] [blame] | 1368 | ModifiedFileSet.EVERYTHING_MODIFIED, |
ajurkowski | 64795c2 | 2022-04-01 10:38:44 -0700 | [diff] [blame] | 1369 | /* trustRemoteArtifacts= */ true, |
| 1370 | (ignored, ignored2) -> {})) |
buchgr | 4992ae2 | 2019-03-20 04:23:32 -0700 | [diff] [blame] | 1371 | .isEmpty(); |
| 1372 | |
| 1373 | // Create the "out1" artifact on the filesystem and test that it invalidates the generating |
| 1374 | // action's SkyKey. |
| 1375 | FileSystemUtils.writeContentAsLatin1(out1.getPath(), "new-foo-content"); |
| 1376 | assertThat( |
ajurkowski | 64795c2 | 2022-04-01 10:38:44 -0700 | [diff] [blame] | 1377 | new FilesystemValueChecker(/*tsgm=*/ null, SyscallCache.NO_CACHE, FSVC_THREADS_FOR_TEST) |
buchgr | 4992ae2 | 2019-03-20 04:23:32 -0700 | [diff] [blame] | 1378 | .getDirtyActionValues( |
| 1379 | evaluator.getValues(), |
| 1380 | /* batchStatter= */ null, |
steinman | 39c00d2 | 2020-03-20 15:23:10 -0700 | [diff] [blame] | 1381 | ModifiedFileSet.EVERYTHING_MODIFIED, |
ajurkowski | 64795c2 | 2022-04-01 10:38:44 -0700 | [diff] [blame] | 1382 | /* trustRemoteArtifacts= */ true, |
| 1383 | (ignored, ignored2) -> {})) |
buchgr | 4992ae2 | 2019-03-20 04:23:32 -0700 | [diff] [blame] | 1384 | .containsExactly(actionKey1); |
| 1385 | } |
| 1386 | |
| 1387 | @Test |
| 1388 | public void testRemoteAndLocalTreeArtifacts() throws Exception { |
| 1389 | // Test that injected remote tree artifacts are trusted by the FileSystemValueChecker |
| 1390 | // and that local files always takes preference over remote files. |
ajurkowski | 280bbe2 | 2020-08-19 11:26:20 -0700 | [diff] [blame] | 1391 | SkyKey actionKey = ActionLookupData.create(ACTION_LOOKUP_KEY, 0); |
buchgr | 4992ae2 | 2019-03-20 04:23:32 -0700 | [diff] [blame] | 1392 | |
| 1393 | SpecialArtifact treeArtifact = createTreeArtifact("dir"); |
| 1394 | treeArtifact.getPath().createDirectoryAndParents(); |
jhorvitz | ed7ec3b | 2020-07-24 15:08:03 -0700 | [diff] [blame] | 1395 | TreeArtifactValue tree = |
| 1396 | TreeArtifactValue.newBuilder(treeArtifact) |
| 1397 | .putChild( |
| 1398 | TreeFileArtifact.createTreeOutput(treeArtifact, "foo"), |
| 1399 | createRemoteFileArtifactValue("foo-content")) |
| 1400 | .putChild( |
| 1401 | TreeFileArtifact.createTreeOutput(treeArtifact, "bar"), |
| 1402 | createRemoteFileArtifactValue("bar-content")) |
| 1403 | .build(); |
buchgr | 4992ae2 | 2019-03-20 04:23:32 -0700 | [diff] [blame] | 1404 | |
jhorvitz | ed7ec3b | 2020-07-24 15:08:03 -0700 | [diff] [blame] | 1405 | differencer.inject(ImmutableMap.of(actionKey, actionValueWithTreeArtifact(treeArtifact, tree))); |
buchgr | 4992ae2 | 2019-03-20 04:23:32 -0700 | [diff] [blame] | 1406 | |
| 1407 | EvaluationContext evaluationContext = |
| 1408 | EvaluationContext.newBuilder() |
| 1409 | .setKeepGoing(false) |
| 1410 | .setNumThreads(1) |
michajlo | 7a485be | 2020-07-30 11:08:46 -0700 | [diff] [blame] | 1411 | .setEventHandler(NullEventHandler.INSTANCE) |
buchgr | 4992ae2 | 2019-03-20 04:23:32 -0700 | [diff] [blame] | 1412 | .build(); |
jhorvitz | 9ad8918 | 2021-12-29 11:57:02 -0800 | [diff] [blame] | 1413 | assertThat(evaluator.evaluate(ImmutableList.of(actionKey), evaluationContext).hasError()) |
buchgr | 4992ae2 | 2019-03-20 04:23:32 -0700 | [diff] [blame] | 1414 | .isFalse(); |
| 1415 | assertThat( |
ajurkowski | 64795c2 | 2022-04-01 10:38:44 -0700 | [diff] [blame] | 1416 | new FilesystemValueChecker(/*tsgm=*/ null, SyscallCache.NO_CACHE, FSVC_THREADS_FOR_TEST) |
buchgr | 4992ae2 | 2019-03-20 04:23:32 -0700 | [diff] [blame] | 1417 | .getDirtyActionValues( |
| 1418 | evaluator.getValues(), |
| 1419 | /* batchStatter= */ null, |
steinman | 39c00d2 | 2020-03-20 15:23:10 -0700 | [diff] [blame] | 1420 | ModifiedFileSet.EVERYTHING_MODIFIED, |
ajurkowski | 64795c2 | 2022-04-01 10:38:44 -0700 | [diff] [blame] | 1421 | /* trustRemoteArtifacts= */ false, |
| 1422 | (ignored, ignored2) -> {})) |
buchgr | 4992ae2 | 2019-03-20 04:23:32 -0700 | [diff] [blame] | 1423 | .isEmpty(); |
| 1424 | |
| 1425 | // Create dir/foo on the local disk and test that it invalidates the associated sky key. |
Googler | 1d8d138 | 2020-05-18 12:10:49 -0700 | [diff] [blame] | 1426 | TreeFileArtifact fooArtifact = TreeFileArtifact.createTreeOutput(treeArtifact, "foo"); |
buchgr | 4992ae2 | 2019-03-20 04:23:32 -0700 | [diff] [blame] | 1427 | FileSystemUtils.writeContentAsLatin1(fooArtifact.getPath(), "new-foo-content"); |
| 1428 | assertThat( |
ajurkowski | 64795c2 | 2022-04-01 10:38:44 -0700 | [diff] [blame] | 1429 | new FilesystemValueChecker(/*tsgm=*/ null, SyscallCache.NO_CACHE, FSVC_THREADS_FOR_TEST) |
buchgr | 4992ae2 | 2019-03-20 04:23:32 -0700 | [diff] [blame] | 1430 | .getDirtyActionValues( |
| 1431 | evaluator.getValues(), |
| 1432 | /* batchStatter= */ null, |
steinman | 39c00d2 | 2020-03-20 15:23:10 -0700 | [diff] [blame] | 1433 | ModifiedFileSet.EVERYTHING_MODIFIED, |
ajurkowski | 64795c2 | 2022-04-01 10:38:44 -0700 | [diff] [blame] | 1434 | /* trustRemoteArtifacts= */ false, |
| 1435 | (ignored, ignored2) -> {})) |
buchgr | 4992ae2 | 2019-03-20 04:23:32 -0700 | [diff] [blame] | 1436 | .containsExactly(actionKey); |
| 1437 | } |
| 1438 | |
Han-Wen Nienhuys | 3b2eae3 | 2015-10-28 16:35:08 +0000 | [diff] [blame] | 1439 | @Test |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 1440 | public void testPropagatesRuntimeExceptions() throws Exception { |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 1441 | Collection<SkyKey> values = |
| 1442 | ImmutableList.of( |
| 1443 | FileValue.key( |
| 1444 | RootedPath.toRootedPath(Root.fromPath(pkgRoot), PathFragment.create("foo")))); |
jhorvitz | 9ad8918 | 2021-12-29 11:57:02 -0800 | [diff] [blame] | 1445 | evaluator.evaluate(values, EVALUATION_OPTIONS); |
janakr | c9f44ec | 2021-07-01 08:13:09 -0700 | [diff] [blame] | 1446 | AtomicReference<Throwable> uncaughtRef = new AtomicReference<>(); |
janakr | 933c301 | 2021-07-13 07:44:15 -0700 | [diff] [blame] | 1447 | CountDownLatch throwableCaught = new CountDownLatch(1); |
janakr | c9f44ec | 2021-07-01 08:13:09 -0700 | [diff] [blame] | 1448 | Thread.UncaughtExceptionHandler uncaughtExceptionHandler = |
janakr | 933c301 | 2021-07-13 07:44:15 -0700 | [diff] [blame] | 1449 | (t, e) -> { |
| 1450 | uncaughtRef.compareAndSet(null, e); |
| 1451 | throwableCaught.countDown(); |
| 1452 | }; |
janakr | c9f44ec | 2021-07-01 08:13:09 -0700 | [diff] [blame] | 1453 | Thread.setDefaultUncaughtExceptionHandler(uncaughtExceptionHandler); |
twerth | 5aaceb5 | 2020-04-07 06:31:56 -0700 | [diff] [blame] | 1454 | FilesystemValueChecker checker = |
| 1455 | new FilesystemValueChecker( |
janakr | 873bf0a | 2022-03-04 13:38:50 -0800 | [diff] [blame] | 1456 | /*tsgm=*/ null, |
| 1457 | SyscallCache.NO_CACHE, |
janakr | 873bf0a | 2022-03-04 13:38:50 -0800 | [diff] [blame] | 1458 | FSVC_THREADS_FOR_TEST); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 1459 | |
Nathan Harmata | 8cd2978 | 2015-11-10 03:24:01 +0000 | [diff] [blame] | 1460 | assertEmptyDiff(getDirtyFilesystemKeys(evaluator, checker)); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 1461 | |
| 1462 | fs.statThrowsRuntimeException = true; |
janakr | c9f44ec | 2021-07-01 08:13:09 -0700 | [diff] [blame] | 1463 | getDirtyFilesystemKeys(evaluator, checker); |
janakr | 933c301 | 2021-07-13 07:44:15 -0700 | [diff] [blame] | 1464 | // Wait for exception handler to trigger (FVC doesn't clean up crashing threads on its own). |
| 1465 | assertThat(throwableCaught.await(TestUtils.WAIT_TIMEOUT_SECONDS, SECONDS)).isTrue(); |
| 1466 | Throwable thrown = uncaughtRef.get(); |
| 1467 | assertThat(thrown).isNotNull(); |
| 1468 | assertThat(thrown).hasMessageThat().isEqualTo("bork"); |
| 1469 | assertThat(thrown).isInstanceOf(RuntimeException.class); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 1470 | } |
| 1471 | |
| 1472 | private static void assertEmptyDiff(Diff diff) { |
| 1473 | assertDiffWithNewValues(diff); |
| 1474 | } |
| 1475 | |
| 1476 | private static void assertDiffWithNewValues(Diff diff, SkyKey... keysWithNewValues) { |
| 1477 | assertThat(diff.changedKeysWithoutNewValues()).isEmpty(); |
| 1478 | assertThat(diff.changedKeysWithNewValues().keySet()) |
| 1479 | .containsExactlyElementsIn(Arrays.asList(keysWithNewValues)); |
| 1480 | } |
| 1481 | |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 1482 | private static FileStatusWithDigest statWithDigest(final Path path, final FileStatus stat) { |
| 1483 | return new FileStatusWithDigest() { |
| 1484 | @Nullable |
| 1485 | @Override |
| 1486 | public byte[] getDigest() throws IOException { |
olaola | bfd1d33 | 2017-06-19 16:55:24 -0400 | [diff] [blame] | 1487 | return path.getDigest(); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 1488 | } |
| 1489 | |
| 1490 | @Override |
| 1491 | public boolean isFile() { |
| 1492 | return stat.isFile(); |
| 1493 | } |
| 1494 | |
| 1495 | @Override |
| 1496 | public boolean isSpecialFile() { |
| 1497 | return stat.isSpecialFile(); |
| 1498 | } |
| 1499 | |
| 1500 | @Override |
| 1501 | public boolean isDirectory() { |
| 1502 | return stat.isDirectory(); |
| 1503 | } |
| 1504 | |
| 1505 | @Override |
| 1506 | public boolean isSymbolicLink() { |
| 1507 | return stat.isSymbolicLink(); |
| 1508 | } |
| 1509 | |
| 1510 | @Override |
| 1511 | public long getSize() throws IOException { |
| 1512 | return stat.getSize(); |
| 1513 | } |
| 1514 | |
| 1515 | @Override |
| 1516 | public long getLastModifiedTime() throws IOException { |
| 1517 | return stat.getLastModifiedTime(); |
| 1518 | } |
| 1519 | |
| 1520 | @Override |
| 1521 | public long getLastChangeTime() throws IOException { |
| 1522 | return stat.getLastChangeTime(); |
| 1523 | } |
| 1524 | |
| 1525 | @Override |
| 1526 | public long getNodeId() throws IOException { |
| 1527 | return stat.getNodeId(); |
| 1528 | } |
| 1529 | }; |
| 1530 | } |
| 1531 | |
Nathan Harmata | 8cd2978 | 2015-11-10 03:24:01 +0000 | [diff] [blame] | 1532 | private static Diff getDirtyFilesystemKeys(MemoizingEvaluator evaluator, |
| 1533 | FilesystemValueChecker checker) throws InterruptedException { |
| 1534 | return checker.getDirtyKeys(evaluator.getValues(), new BasicFilesystemDirtinessChecker()); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 1535 | } |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 1536 | } |