blob: 0a354f2aaf2f4a294aaca9f74eff9e32affba692 [file] [log] [blame]
Damien Martin-Guillerez2988e102016-10-13 20:29:41 +00001// 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
15package com.google.devtools.build.lib.skyframe;
16
jmmvb77b2882020-04-20 13:38:20 -070017import static org.junit.Assume.assumeFalse;
18
jmmvd6095cc2020-05-18 14:33:32 -070019import com.google.common.base.Charsets;
jmmvb77b2882020-04-20 13:38:20 -070020import com.google.common.collect.HashMultimap;
Damien Martin-Guillerez2988e102016-10-13 20:29:41 +000021import com.google.common.collect.ImmutableSet;
jmmvb77b2882020-04-20 13:38:20 -070022import com.google.common.collect.Iterables;
23import com.google.common.collect.Multimap;
janakrc9560212020-05-27 15:07:36 -070024import com.google.common.flogger.GoogleLogger;
Damien Martin-Guillerez2988e102016-10-13 20:29:41 +000025import com.google.devtools.build.lib.skyframe.DiffAwareness.View;
ajurkowski868262e2020-08-19 10:12:20 -070026import com.google.devtools.build.lib.testing.common.FakeOptions;
jmmvb77b2882020-04-20 13:38:20 -070027import com.google.devtools.build.lib.vfs.ModifiedFileSet;
Damien Martin-Guillerez2988e102016-10-13 20:29:41 +000028import com.google.devtools.build.lib.vfs.PathFragment;
juliexxia618a0762018-08-17 08:33:52 -070029import com.google.devtools.common.options.OptionsProvider;
Damien Martin-Guillerez2988e102016-10-13 20:29:41 +000030import java.io.IOException;
Damien Martin-Guillerez2988e102016-10-13 20:29:41 +000031import java.nio.file.FileVisitResult;
32import java.nio.file.Files;
33import java.nio.file.Path;
34import java.nio.file.SimpleFileVisitor;
35import java.nio.file.attribute.BasicFileAttributes;
jmmvb77b2882020-04-20 13:38:20 -070036import java.util.Arrays;
jmmv37131332020-02-12 11:57:34 -080037import java.util.HashSet;
jmmv37131332020-02-12 11:57:34 -080038import java.util.Set;
jmmvb77b2882020-04-20 13:38:20 -070039import java.util.concurrent.CountDownLatch;
40import java.util.concurrent.ExecutorService;
41import java.util.concurrent.Executors;
42import java.util.concurrent.atomic.AtomicReference;
Damien Martin-Guillerez2988e102016-10-13 20:29:41 +000043import org.junit.After;
44import org.junit.Before;
philwoc5690fe2020-05-28 05:43:50 -070045import org.junit.Ignore;
Damien Martin-Guillerez2988e102016-10-13 20:29:41 +000046import org.junit.Test;
47import org.junit.runner.RunWith;
48import org.junit.runners.JUnit4;
49
50/** Tests for {@link MacOSXFsEventsDiffAwareness} */
51@RunWith(JUnit4.class)
52public class MacOSXFsEventsDiffAwarenessTest {
janakrc9560212020-05-27 15:07:36 -070053 private static final GoogleLogger logger = GoogleLogger.forEnclosingClass();
jmmvb77b2882020-04-20 13:38:20 -070054
Damien Martin-Guillerez2988e102016-10-13 20:29:41 +000055 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;
juliexxia618a0762018-08-17 08:33:52 -070076 private OptionsProvider watchFsEnabledProvider;
Damien Martin-Guillerez2988e102016-10-13 20:29:41 +000077
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 Adamsde14ade2016-10-14 14:20:31 +000082 LocalDiffAwareness.Options localDiffOptions = new LocalDiffAwareness.Options();
83 localDiffOptions.watchFS = true;
ajurkowski868262e2020-08-19 10:12:20 -070084 watchFsEnabledProvider = FakeOptions.of(localDiffOptions);
Damien Martin-Guillerez2988e102016-10-13 20:29:41 +000085 }
86
87 @After
88 public void tearDown() throws Exception {
89 underTest.close();
90 rmdirs(watchedPath);
91 }
92
jmmvb77b2882020-04-20 13:38:20 -070093 private void scratchDir(String path) throws IOException {
Damien Martin-Guillerez2988e102016-10-13 20:29:41 +000094 Path p = watchedPath.resolve(path);
jmmvb77b2882020-04-20 13:38:20 -070095 p.toFile().mkdirs();
Damien Martin-Guillerez2988e102016-10-13 20:29:41 +000096 }
97
jmmvd6095cc2020-05-18 14:33:32 -070098 private void scratchFile(String path, String contents) throws IOException {
jmmvb77b2882020-04-20 13:38:20 -070099 Path p = watchedPath.resolve(path);
jmmvd6095cc2020-05-18 14:33:32 -0700100 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-Guillerez2988e102016-10-13 20:29:41 +0000105 }
106
jmmv37131332020-02-12 11:57:34 -0800107 /**
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 */
jmmvb77b2882020-04-20 13:38:20 -0700115 private View assertDiff(View view1, Iterable<String> rawPaths)
jmmv37131332020-02-12 11:57:34 -0800116 throws IncompatibleViewException, BrokenDiffAwarenessException, InterruptedException {
jmmvd6095cc2020-05-18 14:33:32 -0700117 Set<PathFragment> allPaths = new HashSet<>();
jmmv37131332020-02-12 11:57:34 -0800118 for (String path : rawPaths) {
jmmvd6095cc2020-05-18 14:33:32 -0700119 allPaths.add(PathFragment.create(path));
Damien Martin-Guillerez2988e102016-10-13 20:29:41 +0000120 }
jmmvd6095cc2020-05-18 14:33:32 -0700121 Set<PathFragment> pathsYetToBeSeen = new HashSet<>(allPaths);
jmmv37131332020-02-12 11:57:34 -0800122
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
jmmvb77b2882020-04-20 13:38:20 -0700132 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();
jmmvd6095cc2020-05-18 14:33:32 -0700140 allPaths.removeAll(modifiedSourceFiles);
jmmv37131332020-02-12 11:57:34 -0800141 pathsYetToBeSeen.removeAll(modifiedSourceFiles);
142 if (pathsYetToBeSeen.isEmpty()) {
jmmvb03765a2020-05-18 08:00:54 -0700143 // 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.
jmmvd6095cc2020-05-18 14:33:32 -0700145 if (!allPaths.isEmpty()) {
146 throw new AssertionError("Paths " + allPaths + " unexpectedly reported as modified");
jmmvb03765a2020-05-18 08:00:54 -0700147 }
jmmv37131332020-02-12 11:57:34 -0800148 return view2;
149 }
150
151 if (attempts == 600) {
152 throw new AssertionError("Paths " + pathsYetToBeSeen + " not found as modified");
153 }
janakrc9560212020-05-27 15:07:36 -0700154 logger.atInfo().log("Still have to see %d paths", pathsYetToBeSeen.size());
jmmv37131332020-02-12 11:57:34 -0800155 Thread.sleep(100);
156 attempts++;
157 view1 = view2; // getDiff requires views to be sequential if we want to get meaningful data.
158 }
Damien Martin-Guillerez2988e102016-10-13 20:29:41 +0000159 }
160
161 @Test
philwoc5690fe2020-05-28 05:43:50 -0700162 @Ignore("Test is flaky; see https://github.com/bazelbuild/bazel/issues/10776")
Damien Martin-Guillerez2988e102016-10-13 20:29:41 +0000163 public void testSimple() throws Exception {
Ulf Adamsde14ade2016-10-14 14:20:31 +0000164 View view1 = underTest.getCurrentView(watchFsEnabledProvider);
jmmv37131332020-02-12 11:57:34 -0800165
jmmvb77b2882020-04-20 13:38:20 -0700166 scratchDir("a/b");
Damien Martin-Guillerez2988e102016-10-13 20:29:41 +0000167 scratchFile("a/b/c");
jmmvb77b2882020-04-20 13:38:20 -0700168 scratchDir("b/c");
Damien Martin-Guillerez2988e102016-10-13 20:29:41 +0000169 scratchFile("b/c/d");
jmmvb77b2882020-04-20 13:38:20 -0700170 View view2 = assertDiff(view1, Arrays.asList("a", "a/b", "a/b/c", "b", "b/c", "b/c/d"));
jmmv37131332020-02-12 11:57:34 -0800171
Damien Martin-Guillerez2988e102016-10-13 20:29:41 +0000172 rmdirs(watchedPath.resolve("a"));
173 rmdirs(watchedPath.resolve("b"));
jmmvb77b2882020-04-20 13:38:20 -0700174 assertDiff(view2, Arrays.asList("a", "a/b", "a/b/c", "b", "b/c", "b/c/d"));
175 }
176
177 @Test
philwoc5690fe2020-05-28 05:43:50 -0700178 @Ignore("Test is flaky; see https://github.com/bazelbuild/bazel/issues/10776")
jmmvd6095cc2020-05-18 14:33:32 -0700179 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
philwoc5690fe2020-05-28 05:43:50 -0700193 @Ignore("Test is flaky; see https://github.com/bazelbuild/bazel/issues/10776")
jmmvb77b2882020-04-20 13:38:20 -0700194 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.
janakrc9560212020-05-27 15:07:36 -0700201 logger.atInfo().log("Starting file creation under %s", watchedPath);
jmmvb77b2882020-04-20 13:38:20 -0700202 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-Guillerez2988e102016-10-13 20:29:41 +0000239 }
Damien Martin-Guillerez2988e102016-10-13 20:29:41 +0000240}