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