Search in sources :

Example 1 with FileSystem

use of com.google.devtools.build.lib.vfs.FileSystem in project bazel by bazelbuild.

the class ParallelBuilderTest method testUpdateCacheError.

@Test
public void testUpdateCacheError() throws Exception {
    FileSystem fs = new InMemoryFileSystem() {

        @Override
        public FileStatus stat(Path path, boolean followSymlinks) throws IOException {
            final FileStatus stat = super.stat(path, followSymlinks);
            if (path.toString().endsWith("/out/foo")) {
                return new FileStatus() {

                    private final FileStatus original = stat;

                    @Override
                    public boolean isSymbolicLink() {
                        return original.isSymbolicLink();
                    }

                    @Override
                    public boolean isFile() {
                        return original.isFile();
                    }

                    @Override
                    public boolean isDirectory() {
                        return original.isDirectory();
                    }

                    @Override
                    public boolean isSpecialFile() {
                        return original.isSpecialFile();
                    }

                    @Override
                    public long getSize() throws IOException {
                        return original.getSize();
                    }

                    @Override
                    public long getNodeId() throws IOException {
                        return original.getNodeId();
                    }

                    @Override
                    public long getLastModifiedTime() throws IOException {
                        throw new IOException();
                    }

                    @Override
                    public long getLastChangeTime() throws IOException {
                        return original.getLastChangeTime();
                    }
                };
            }
            return stat;
        }
    };
    Artifact foo = createDerivedArtifact(fs, "foo");
    registerAction(new TestAction(TestAction.NO_EFFECT, emptySet, ImmutableList.of(foo)));
    reporter.removeHandler(failFastHandler);
    try {
        buildArtifacts(foo);
        fail("Expected to fail");
    } catch (BuildFailedException e) {
        assertContainsEvent("not all outputs were created or valid");
    }
}
Also used : Path(com.google.devtools.build.lib.vfs.Path) BuildFailedException(com.google.devtools.build.lib.actions.BuildFailedException) FileStatus(com.google.devtools.build.lib.vfs.FileStatus) FileSystem(com.google.devtools.build.lib.vfs.FileSystem) InMemoryFileSystem(com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem) InMemoryFileSystem(com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem) IOException(java.io.IOException) Artifact(com.google.devtools.build.lib.actions.Artifact) TestAction(com.google.devtools.build.lib.actions.util.TestAction) Test(org.junit.Test)

Example 2 with FileSystem

use of com.google.devtools.build.lib.vfs.FileSystem in project bazel by bazelbuild.

the class BlazeDirectoriesTest method testCreatingDirectories.

@Test
public void testCreatingDirectories() {
    FileSystem fs = scratch.getFileSystem();
    Path installBase = fs.getPath("/my/install");
    Path outputBase = fs.getPath("/my/output");
    Path workspace = fs.getPath("/my/ws");
    BlazeDirectories directories = new BlazeDirectories(installBase, outputBase, workspace, "foo");
    assertEquals(directories.getExecRoot(), outputBase.getChild("ws"));
    workspace = null;
    directories = new BlazeDirectories(installBase, outputBase, workspace, "foo");
    assertEquals(directories.getExecRoot(), outputBase.getChild(BlazeDirectories.DEFAULT_EXEC_ROOT));
    workspace = fs.getPath("/");
    directories = new BlazeDirectories(installBase, outputBase, workspace, "foo");
    assertEquals(directories.getExecRoot(), outputBase.getChild(BlazeDirectories.DEFAULT_EXEC_ROOT));
}
Also used : Path(com.google.devtools.build.lib.vfs.Path) FileSystem(com.google.devtools.build.lib.vfs.FileSystem) Test(org.junit.Test)

Example 3 with FileSystem

use of com.google.devtools.build.lib.vfs.FileSystem in project bazel by bazelbuild.

the class DigestUtilsTest method assertDigestCalculationConcurrency.

private static void assertDigestCalculationConcurrency(boolean expectConcurrent, final boolean fastDigest, final int fileSize1, final int fileSize2, HashFunction hf) throws Exception {
    // Used to block test threads.
    final CountDownLatch barrierLatch = new CountDownLatch(2);
    // Used to block main thread.
    final CountDownLatch readyLatch = new CountDownLatch(1);
    FileSystem myfs = new InMemoryFileSystem(BlazeClock.instance()) {

        @Override
        protected byte[] getMD5Digest(Path path) throws IOException {
            try {
                barrierLatch.countDown();
                readyLatch.countDown();
                // Either both threads will be inside getMD5Digest at the same time or they
                // both will be blocked.
                barrierLatch.await();
            } catch (Exception e) {
                throw new IOException(e);
            }
            return super.getMD5Digest(path);
        }

        @Override
        protected byte[] getSHA1Digest(Path path) throws IOException {
            try {
                barrierLatch.countDown();
                readyLatch.countDown();
                // Either both threads will be inside getSHA1Digest at the same time or they
                // both will be blocked.
                barrierLatch.await();
            } catch (Exception e) {
                throw new IOException(e);
            }
            return super.getSHA1Digest(path);
        }

        @Override
        protected byte[] getFastDigest(Path path, HashFunction hashFunction) throws IOException {
            return fastDigest ? super.getDigest(path, hashFunction) : null;
        }
    };
    FileSystem.setDigestFunctionForTesting(hf);
    final Path myFile1 = myfs.getPath("/f1.dat");
    final Path myFile2 = myfs.getPath("/f2.dat");
    FileSystemUtils.writeContentAsLatin1(myFile1, Strings.repeat("a", fileSize1));
    FileSystemUtils.writeContentAsLatin1(myFile2, Strings.repeat("b", fileSize2));
    TestThread thread1 = new TestThread() {

        @Override
        public void runTest() throws Exception {
            DigestUtils.getDigestOrFail(myFile1, fileSize1);
        }
    };
    TestThread thread2 = new TestThread() {

        @Override
        public void runTest() throws Exception {
            DigestUtils.getDigestOrFail(myFile2, fileSize2);
        }
    };
    thread1.start();
    thread2.start();
    if (!expectConcurrent) {
        // Synchronized case.
        // Wait until at least one thread reached getMD5Digest().
        assertTrue(readyLatch.await(TestUtils.WAIT_TIMEOUT_SECONDS, TimeUnit.SECONDS));
        // Only 1 thread should be inside getMD5Digest().
        assertEquals(1, barrierLatch.getCount());
        // Release barrier latch, allowing both threads to proceed.
        barrierLatch.countDown();
    }
    // Test successful execution within 5 seconds.
    thread1.joinAndAssertState(TestUtils.WAIT_TIMEOUT_MILLISECONDS);
    thread2.joinAndAssertState(TestUtils.WAIT_TIMEOUT_MILLISECONDS);
}
Also used : Path(com.google.devtools.build.lib.vfs.Path) HashFunction(com.google.devtools.build.lib.vfs.FileSystem.HashFunction) TestThread(com.google.devtools.build.lib.testutil.TestThread) InMemoryFileSystem(com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem) FileSystem(com.google.devtools.build.lib.vfs.FileSystem) InMemoryFileSystem(com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException)

Example 4 with FileSystem

use of com.google.devtools.build.lib.vfs.FileSystem in project bazel by bazelbuild.

the class DigestUtilsTest method assertRecoverFromMalformedDigest.

public void assertRecoverFromMalformedDigest(HashFunction... hashFunctions) throws Exception {
    final byte[] malformed = { 0, 0, 0 };
    FileSystem myFS = new InMemoryFileSystem(BlazeClock.instance()) {

        @Override
        protected byte[] getFastDigest(Path path, HashFunction hashFunction) throws IOException {
            // Digest functions have more than 3 bytes, usually at least 16.
            return malformed;
        }
    };
    Path path = myFS.getPath("/file");
    FileSystemUtils.writeContentAsLatin1(path, "a");
    for (HashFunction hf : hashFunctions) {
        FileSystem.setDigestFunctionForTesting(hf);
        byte[] result = DigestUtils.getDigestOrFail(path, 1);
        assertArrayEquals(path.getDigest(), result);
        assertNotSame(malformed, result);
        assertTrue(path.isValidDigest(result));
    }
}
Also used : Path(com.google.devtools.build.lib.vfs.Path) HashFunction(com.google.devtools.build.lib.vfs.FileSystem.HashFunction) InMemoryFileSystem(com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem) FileSystem(com.google.devtools.build.lib.vfs.FileSystem) InMemoryFileSystem(com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem)

Example 5 with FileSystem

use of com.google.devtools.build.lib.vfs.FileSystem in project bazel by bazelbuild.

the class DigestUtilsTest method testCache.

@Test
public void testCache() throws Exception {
    final AtomicInteger getFastDigestCounter = new AtomicInteger(0);
    final AtomicInteger getDigestCounter = new AtomicInteger(0);
    FileSystem tracingFileSystem = new InMemoryFileSystem(BlazeClock.instance()) {

        @Override
        protected byte[] getFastDigest(Path path, HashFunction hashFunction) throws IOException {
            getFastDigestCounter.incrementAndGet();
            return null;
        }

        @Override
        protected byte[] getDigest(Path path) throws IOException {
            getDigestCounter.incrementAndGet();
            return super.getDigest(path);
        }
    };
    DigestUtils.configureCache(2);
    final Path file1 = tracingFileSystem.getPath("/1.txt");
    final Path file2 = tracingFileSystem.getPath("/2.txt");
    final Path file3 = tracingFileSystem.getPath("/3.txt");
    FileSystemUtils.writeContentAsLatin1(file1, "some contents");
    FileSystemUtils.writeContentAsLatin1(file2, "some other contents");
    FileSystemUtils.writeContentAsLatin1(file3, "and something else");
    byte[] digest1 = DigestUtils.getDigestOrFail(file1, file1.getFileSize());
    assertEquals(1, getFastDigestCounter.get());
    assertEquals(1, getDigestCounter.get());
    new CacheStatsChecker().evictionCount(0).hitCount(0).missCount(1).check();
    byte[] digest2 = DigestUtils.getDigestOrFail(file1, file1.getFileSize());
    assertEquals(2, getFastDigestCounter.get());
    assertEquals(1, getDigestCounter.get());
    new CacheStatsChecker().evictionCount(0).hitCount(1).missCount(1).check();
    assertArrayEquals(digest1, digest2);
    // Evict the digest for the previous file.
    DigestUtils.getDigestOrFail(file2, file2.getFileSize());
    DigestUtils.getDigestOrFail(file3, file3.getFileSize());
    new CacheStatsChecker().evictionCount(1).hitCount(1).missCount(3).check();
    // And now try to recompute it.
    byte[] digest3 = DigestUtils.getDigestOrFail(file1, file1.getFileSize());
    new CacheStatsChecker().evictionCount(2).hitCount(1).missCount(4).check();
    assertArrayEquals(digest1, digest3);
}
Also used : Path(com.google.devtools.build.lib.vfs.Path) HashFunction(com.google.devtools.build.lib.vfs.FileSystem.HashFunction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) InMemoryFileSystem(com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem) FileSystem(com.google.devtools.build.lib.vfs.FileSystem) InMemoryFileSystem(com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem) Test(org.junit.Test)

Aggregations

FileSystem (com.google.devtools.build.lib.vfs.FileSystem)17 Path (com.google.devtools.build.lib.vfs.Path)17 InMemoryFileSystem (com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem)11 IOException (java.io.IOException)7 Test (org.junit.Test)6 PathFragment (com.google.devtools.build.lib.vfs.PathFragment)4 RootedPath (com.google.devtools.build.lib.vfs.RootedPath)4 TestAction (com.google.devtools.build.lib.actions.util.TestAction)3 HashFunction (com.google.devtools.build.lib.vfs.FileSystem.HashFunction)3 DirectoryListingValue (com.google.devtools.build.lib.skyframe.DirectoryListingValue)2 Dirent (com.google.devtools.build.lib.vfs.Dirent)2 FileNotFoundException (java.io.FileNotFoundException)2 Before (org.junit.Before)2 Predicate (com.google.common.base.Predicate)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Artifact (com.google.devtools.build.lib.actions.Artifact)1 SpecialArtifact (com.google.devtools.build.lib.actions.Artifact.SpecialArtifact)1 BuildFailedException (com.google.devtools.build.lib.actions.BuildFailedException)1 BlazeDirectories (com.google.devtools.build.lib.analysis.BlazeDirectories)1 ServerDirectories (com.google.devtools.build.lib.analysis.ServerDirectories)1