Damien Martin-Guillerez | 2988e10 | 2016-10-13 20:29:41 +0000 | [diff] [blame] | 1 | // Copyright 2016 The Bazel Authors. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package com.google.devtools.build.lib.skyframe; |
| 16 | |
jmmv | b77b288 | 2020-04-20 13:38:20 -0700 | [diff] [blame] | 17 | import static org.junit.Assume.assumeFalse; |
| 18 | |
jmmv | d6095cc | 2020-05-18 14:33:32 -0700 | [diff] [blame] | 19 | import com.google.common.base.Charsets; |
jmmv | b77b288 | 2020-04-20 13:38:20 -0700 | [diff] [blame] | 20 | import com.google.common.collect.HashMultimap; |
Damien Martin-Guillerez | 2988e10 | 2016-10-13 20:29:41 +0000 | [diff] [blame] | 21 | import com.google.common.collect.ImmutableSet; |
jmmv | b77b288 | 2020-04-20 13:38:20 -0700 | [diff] [blame] | 22 | import com.google.common.collect.Iterables; |
| 23 | import com.google.common.collect.Multimap; |
janakr | c956021 | 2020-05-27 15:07:36 -0700 | [diff] [blame] | 24 | import com.google.common.flogger.GoogleLogger; |
Damien Martin-Guillerez | 2988e10 | 2016-10-13 20:29:41 +0000 | [diff] [blame] | 25 | import com.google.devtools.build.lib.skyframe.DiffAwareness.View; |
ajurkowski | 868262e | 2020-08-19 10:12:20 -0700 | [diff] [blame] | 26 | import com.google.devtools.build.lib.testing.common.FakeOptions; |
jmmv | b77b288 | 2020-04-20 13:38:20 -0700 | [diff] [blame] | 27 | import com.google.devtools.build.lib.vfs.ModifiedFileSet; |
Damien Martin-Guillerez | 2988e10 | 2016-10-13 20:29:41 +0000 | [diff] [blame] | 28 | import com.google.devtools.build.lib.vfs.PathFragment; |
juliexxia | 618a076 | 2018-08-17 08:33:52 -0700 | [diff] [blame] | 29 | import com.google.devtools.common.options.OptionsProvider; |
Damien Martin-Guillerez | 2988e10 | 2016-10-13 20:29:41 +0000 | [diff] [blame] | 30 | import java.io.IOException; |
Damien Martin-Guillerez | 2988e10 | 2016-10-13 20:29:41 +0000 | [diff] [blame] | 31 | import java.nio.file.FileVisitResult; |
| 32 | import java.nio.file.Files; |
| 33 | import java.nio.file.Path; |
| 34 | import java.nio.file.SimpleFileVisitor; |
| 35 | import java.nio.file.attribute.BasicFileAttributes; |
jmmv | b77b288 | 2020-04-20 13:38:20 -0700 | [diff] [blame] | 36 | import java.util.Arrays; |
jmmv | 3713133 | 2020-02-12 11:57:34 -0800 | [diff] [blame] | 37 | import java.util.HashSet; |
jmmv | 3713133 | 2020-02-12 11:57:34 -0800 | [diff] [blame] | 38 | import java.util.Set; |
jmmv | b77b288 | 2020-04-20 13:38:20 -0700 | [diff] [blame] | 39 | import java.util.concurrent.CountDownLatch; |
| 40 | import java.util.concurrent.ExecutorService; |
| 41 | import java.util.concurrent.Executors; |
| 42 | import java.util.concurrent.atomic.AtomicReference; |
Damien Martin-Guillerez | 2988e10 | 2016-10-13 20:29:41 +0000 | [diff] [blame] | 43 | import org.junit.After; |
| 44 | import org.junit.Before; |
philwo | c5690fe | 2020-05-28 05:43:50 -0700 | [diff] [blame] | 45 | import org.junit.Ignore; |
Damien Martin-Guillerez | 2988e10 | 2016-10-13 20:29:41 +0000 | [diff] [blame] | 46 | import org.junit.Test; |
| 47 | import org.junit.runner.RunWith; |
| 48 | import org.junit.runners.JUnit4; |
| 49 | |
| 50 | /** Tests for {@link MacOSXFsEventsDiffAwareness} */ |
| 51 | @RunWith(JUnit4.class) |
| 52 | public class MacOSXFsEventsDiffAwarenessTest { |
janakr | c956021 | 2020-05-27 15:07:36 -0700 | [diff] [blame] | 53 | private static final GoogleLogger logger = GoogleLogger.forEnclosingClass(); |
jmmv | b77b288 | 2020-04-20 13:38:20 -0700 | [diff] [blame] | 54 | |
Damien Martin-Guillerez | 2988e10 | 2016-10-13 20:29:41 +0000 | [diff] [blame] | 55 | private static void rmdirs(Path directory) throws IOException { |
| 56 | Files.walkFileTree( |
| 57 | directory, |
| 58 | new SimpleFileVisitor<Path>() { |
| 59 | @Override |
| 60 | public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) |
| 61 | throws IOException { |
| 62 | Files.delete(file); |
| 63 | return FileVisitResult.CONTINUE; |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { |
| 68 | Files.delete(dir); |
| 69 | return FileVisitResult.CONTINUE; |
| 70 | } |
| 71 | }); |
| 72 | } |
| 73 | |
| 74 | private MacOSXFsEventsDiffAwareness underTest; |
| 75 | private Path watchedPath; |
juliexxia | 618a076 | 2018-08-17 08:33:52 -0700 | [diff] [blame] | 76 | private OptionsProvider watchFsEnabledProvider; |
Damien Martin-Guillerez | 2988e10 | 2016-10-13 20:29:41 +0000 | [diff] [blame] | 77 | |
| 78 | @Before |
| 79 | public void setUp() throws Exception { |
| 80 | watchedPath = com.google.common.io.Files.createTempDir().getCanonicalFile().toPath(); |
| 81 | underTest = new MacOSXFsEventsDiffAwareness(watchedPath.toString()); |
Ulf Adams | de14ade | 2016-10-14 14:20:31 +0000 | [diff] [blame] | 82 | LocalDiffAwareness.Options localDiffOptions = new LocalDiffAwareness.Options(); |
| 83 | localDiffOptions.watchFS = true; |
ajurkowski | 868262e | 2020-08-19 10:12:20 -0700 | [diff] [blame] | 84 | watchFsEnabledProvider = FakeOptions.of(localDiffOptions); |
Damien Martin-Guillerez | 2988e10 | 2016-10-13 20:29:41 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | @After |
| 88 | public void tearDown() throws Exception { |
| 89 | underTest.close(); |
| 90 | rmdirs(watchedPath); |
| 91 | } |
| 92 | |
jmmv | b77b288 | 2020-04-20 13:38:20 -0700 | [diff] [blame] | 93 | private void scratchDir(String path) throws IOException { |
Damien Martin-Guillerez | 2988e10 | 2016-10-13 20:29:41 +0000 | [diff] [blame] | 94 | Path p = watchedPath.resolve(path); |
jmmv | b77b288 | 2020-04-20 13:38:20 -0700 | [diff] [blame] | 95 | p.toFile().mkdirs(); |
Damien Martin-Guillerez | 2988e10 | 2016-10-13 20:29:41 +0000 | [diff] [blame] | 96 | } |
| 97 | |
jmmv | d6095cc | 2020-05-18 14:33:32 -0700 | [diff] [blame] | 98 | private void scratchFile(String path, String contents) throws IOException { |
jmmv | b77b288 | 2020-04-20 13:38:20 -0700 | [diff] [blame] | 99 | Path p = watchedPath.resolve(path); |
jmmv | d6095cc | 2020-05-18 14:33:32 -0700 | [diff] [blame] | 100 | com.google.common.io.Files.write(contents.getBytes(Charsets.UTF_8), p.toFile()); |
| 101 | } |
| 102 | |
| 103 | private void scratchFile(String path) throws IOException { |
| 104 | scratchFile(path, ""); |
Damien Martin-Guillerez | 2988e10 | 2016-10-13 20:29:41 +0000 | [diff] [blame] | 105 | } |
| 106 | |
jmmv | 3713133 | 2020-02-12 11:57:34 -0800 | [diff] [blame] | 107 | /** |
| 108 | * Checks that the union of the diffs between the current view and each member of some consecutive |
| 109 | * sequence of views is the specific set of given files. |
| 110 | * |
| 111 | * @param view1 the view to compare to |
| 112 | * @param rawPaths the files to expect in the view |
| 113 | * @return the new view |
| 114 | */ |
jmmv | b77b288 | 2020-04-20 13:38:20 -0700 | [diff] [blame] | 115 | private View assertDiff(View view1, Iterable<String> rawPaths) |
jmmv | 3713133 | 2020-02-12 11:57:34 -0800 | [diff] [blame] | 116 | throws IncompatibleViewException, BrokenDiffAwarenessException, InterruptedException { |
jmmv | d6095cc | 2020-05-18 14:33:32 -0700 | [diff] [blame] | 117 | Set<PathFragment> allPaths = new HashSet<>(); |
jmmv | 3713133 | 2020-02-12 11:57:34 -0800 | [diff] [blame] | 118 | for (String path : rawPaths) { |
jmmv | d6095cc | 2020-05-18 14:33:32 -0700 | [diff] [blame] | 119 | allPaths.add(PathFragment.create(path)); |
Damien Martin-Guillerez | 2988e10 | 2016-10-13 20:29:41 +0000 | [diff] [blame] | 120 | } |
jmmv | d6095cc | 2020-05-18 14:33:32 -0700 | [diff] [blame] | 121 | Set<PathFragment> pathsYetToBeSeen = new HashSet<>(allPaths); |
jmmv | 3713133 | 2020-02-12 11:57:34 -0800 | [diff] [blame] | 122 | |
| 123 | // fsevents may be delayed (especially under machine load), which means that we may not notice |
| 124 | // all file system changes in one go. Try enough times (multiple seconds) for the events to be |
| 125 | // delivered. Given that each time we call getCurrentView we may get a subset of the total |
| 126 | // events we expect, track the events we have already seen by subtracting them from the |
| 127 | // pathsYetToBeSeen set. |
| 128 | int attempts = 0; |
| 129 | for (; ; ) { |
| 130 | View view2 = underTest.getCurrentView(watchFsEnabledProvider); |
| 131 | |
jmmv | b77b288 | 2020-04-20 13:38:20 -0700 | [diff] [blame] | 132 | ModifiedFileSet diff = underTest.getDiff(view1, view2); |
| 133 | // If fsevents lost events (e.g. because we weren't fast enough processing them or because |
| 134 | // too many happened at the same time), there is nothing we can do. Yes, this means that if |
| 135 | // our fsevents monitor always returns "everything modified", we aren't really testing |
| 136 | // anything here... but let's assume we don't have such an obvious bug... |
| 137 | assumeFalse("Lost events; diff unknown", diff.equals(ModifiedFileSet.EVERYTHING_MODIFIED)); |
| 138 | |
| 139 | ImmutableSet<PathFragment> modifiedSourceFiles = diff.modifiedSourceFiles(); |
jmmv | d6095cc | 2020-05-18 14:33:32 -0700 | [diff] [blame] | 140 | allPaths.removeAll(modifiedSourceFiles); |
jmmv | 3713133 | 2020-02-12 11:57:34 -0800 | [diff] [blame] | 141 | pathsYetToBeSeen.removeAll(modifiedSourceFiles); |
| 142 | if (pathsYetToBeSeen.isEmpty()) { |
jmmv | b03765a | 2020-05-18 08:00:54 -0700 | [diff] [blame] | 143 | // Found all paths that we wanted to see as modified so now check that we didn't get any |
| 144 | // extra paths we did not expect. |
jmmv | d6095cc | 2020-05-18 14:33:32 -0700 | [diff] [blame] | 145 | if (!allPaths.isEmpty()) { |
| 146 | throw new AssertionError("Paths " + allPaths + " unexpectedly reported as modified"); |
jmmv | b03765a | 2020-05-18 08:00:54 -0700 | [diff] [blame] | 147 | } |
jmmv | 3713133 | 2020-02-12 11:57:34 -0800 | [diff] [blame] | 148 | return view2; |
| 149 | } |
| 150 | |
| 151 | if (attempts == 600) { |
| 152 | throw new AssertionError("Paths " + pathsYetToBeSeen + " not found as modified"); |
| 153 | } |
janakr | c956021 | 2020-05-27 15:07:36 -0700 | [diff] [blame] | 154 | logger.atInfo().log("Still have to see %d paths", pathsYetToBeSeen.size()); |
jmmv | 3713133 | 2020-02-12 11:57:34 -0800 | [diff] [blame] | 155 | Thread.sleep(100); |
| 156 | attempts++; |
| 157 | view1 = view2; // getDiff requires views to be sequential if we want to get meaningful data. |
| 158 | } |
Damien Martin-Guillerez | 2988e10 | 2016-10-13 20:29:41 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | @Test |
philwo | c5690fe | 2020-05-28 05:43:50 -0700 | [diff] [blame] | 162 | @Ignore("Test is flaky; see https://github.com/bazelbuild/bazel/issues/10776") |
Damien Martin-Guillerez | 2988e10 | 2016-10-13 20:29:41 +0000 | [diff] [blame] | 163 | public void testSimple() throws Exception { |
Ulf Adams | de14ade | 2016-10-14 14:20:31 +0000 | [diff] [blame] | 164 | View view1 = underTest.getCurrentView(watchFsEnabledProvider); |
jmmv | 3713133 | 2020-02-12 11:57:34 -0800 | [diff] [blame] | 165 | |
jmmv | b77b288 | 2020-04-20 13:38:20 -0700 | [diff] [blame] | 166 | scratchDir("a/b"); |
Damien Martin-Guillerez | 2988e10 | 2016-10-13 20:29:41 +0000 | [diff] [blame] | 167 | scratchFile("a/b/c"); |
jmmv | b77b288 | 2020-04-20 13:38:20 -0700 | [diff] [blame] | 168 | scratchDir("b/c"); |
Damien Martin-Guillerez | 2988e10 | 2016-10-13 20:29:41 +0000 | [diff] [blame] | 169 | scratchFile("b/c/d"); |
jmmv | b77b288 | 2020-04-20 13:38:20 -0700 | [diff] [blame] | 170 | View view2 = assertDiff(view1, Arrays.asList("a", "a/b", "a/b/c", "b", "b/c", "b/c/d")); |
jmmv | 3713133 | 2020-02-12 11:57:34 -0800 | [diff] [blame] | 171 | |
Damien Martin-Guillerez | 2988e10 | 2016-10-13 20:29:41 +0000 | [diff] [blame] | 172 | rmdirs(watchedPath.resolve("a")); |
| 173 | rmdirs(watchedPath.resolve("b")); |
jmmv | b77b288 | 2020-04-20 13:38:20 -0700 | [diff] [blame] | 174 | assertDiff(view2, Arrays.asList("a", "a/b", "a/b/c", "b", "b/c", "b/c/d")); |
| 175 | } |
| 176 | |
| 177 | @Test |
philwo | c5690fe | 2020-05-28 05:43:50 -0700 | [diff] [blame] | 178 | @Ignore("Test is flaky; see https://github.com/bazelbuild/bazel/issues/10776") |
jmmv | d6095cc | 2020-05-18 14:33:32 -0700 | [diff] [blame] | 179 | public void testRenameDirectory() throws Exception { |
| 180 | scratchDir("dir1"); |
| 181 | scratchFile("dir1/file.c", "first"); |
| 182 | scratchDir("dir2"); |
| 183 | scratchFile("dir2/file.c", "second"); |
| 184 | View view1 = underTest.getCurrentView(watchFsEnabledProvider); |
| 185 | |
| 186 | Files.move(watchedPath.resolve("dir1"), watchedPath.resolve("dir3")); |
| 187 | Files.move(watchedPath.resolve("dir2"), watchedPath.resolve("dir1")); |
| 188 | assertDiff( |
| 189 | view1, Arrays.asList("dir1", "dir1/file.c", "dir2", "dir2/file.c", "dir3", "dir3/file.c")); |
| 190 | } |
| 191 | |
| 192 | @Test |
philwo | c5690fe | 2020-05-28 05:43:50 -0700 | [diff] [blame] | 193 | @Ignore("Test is flaky; see https://github.com/bazelbuild/bazel/issues/10776") |
jmmv | b77b288 | 2020-04-20 13:38:20 -0700 | [diff] [blame] | 194 | public void testStress() throws Exception { |
| 195 | View view1 = underTest.getCurrentView(watchFsEnabledProvider); |
| 196 | |
| 197 | // Attempt to cause fsevents to drop events by performing a lot of concurrent file accesses |
| 198 | // which then may result in our own callback in fsevents.cc not being able to keep up. |
| 199 | // There is no guarantee that we'll trigger this condition, but on 2020-02-28 on a Mac Pro |
| 200 | // 2013, this happened pretty predictably with the settings below. |
janakr | c956021 | 2020-05-27 15:07:36 -0700 | [diff] [blame] | 201 | logger.atInfo().log("Starting file creation under %s", watchedPath); |
jmmv | b77b288 | 2020-04-20 13:38:20 -0700 | [diff] [blame] | 202 | ExecutorService executor = Executors.newCachedThreadPool(); |
| 203 | int nThreads = 100; |
| 204 | int nFilesPerThread = 100; |
| 205 | Multimap<String, String> dirToFilesToCreate = HashMultimap.create(); |
| 206 | for (int i = 0; i < nThreads; i++) { |
| 207 | String dir = "" + i; |
| 208 | for (int j = 0; j < nFilesPerThread; j++) { |
| 209 | String file = dir + "/" + j; |
| 210 | dirToFilesToCreate.put(dir, file); |
| 211 | } |
| 212 | } |
| 213 | CountDownLatch latch = new CountDownLatch(nThreads); |
| 214 | AtomicReference<IOException> firstError = new AtomicReference<>(null); |
| 215 | dirToFilesToCreate |
| 216 | .asMap() |
| 217 | .forEach( |
| 218 | (dir, files) -> |
| 219 | executor.submit( |
| 220 | () -> { |
| 221 | try { |
| 222 | scratchDir(dir); |
| 223 | for (String file : files) { |
| 224 | scratchFile(file); |
| 225 | } |
| 226 | } catch (IOException e) { |
| 227 | firstError.compareAndSet(null, e); |
| 228 | } |
| 229 | latch.countDown(); |
| 230 | })); |
| 231 | latch.await(); |
| 232 | executor.shutdown(); |
| 233 | IOException e = firstError.get(); |
| 234 | if (e != null) { |
| 235 | throw e; |
| 236 | } |
| 237 | |
| 238 | assertDiff(view1, Iterables.concat(dirToFilesToCreate.keySet(), dirToFilesToCreate.values())); |
Damien Martin-Guillerez | 2988e10 | 2016-10-13 20:29:41 +0000 | [diff] [blame] | 239 | } |
Damien Martin-Guillerez | 2988e10 | 2016-10-13 20:29:41 +0000 | [diff] [blame] | 240 | } |