Search in sources :

Example 6 with ProjectFileHashCache

use of com.facebook.buck.util.cache.ProjectFileHashCache in project buck by facebook.

the class RecordingFileHashLoaderTest method testRecordsDirectoryAndRecursivelyRecordsChildren.

@Test
public void testRecordsDirectoryAndRecursivelyRecordsChildren() throws IOException {
    // Scenario:
    // /a - folder
    // /a/b - folder
    // /a/b/c - file
    // /a/b/d - folder
    // /a/e - file
    // => entries for all files and folders.
    // => entries for dirs /a and /a/b list their direct children
    assumeTrue(!Platform.detect().equals(Platform.WINDOWS));
    ProjectFilesystem fs = new ProjectFilesystem(projectDir.getRoot().toPath());
    Path pathDirA = Files.createDirectories(fs.getRootPath().resolve("a"));
    Files.createDirectories(fs.getRootPath().resolve("a/b"));
    Files.createFile(fs.getRootPath().resolve("a/b/c"));
    Files.createDirectories(fs.getRootPath().resolve("a/b/d"));
    Files.createFile(fs.getRootPath().resolve("a/e"));
    RecordedFileHashes recordedFileHashes = new RecordedFileHashes(0);
    BuildJobStateFileHashes fileHashes = recordedFileHashes.getRemoteFileHashes();
    ProjectFileHashCache delegateCacheMock = EasyMock.createMock(ProjectFileHashCache.class);
    expect(delegateCacheMock.getFilesystem()).andReturn(fs);
    expect(delegateCacheMock.get(anyObject(Path.class))).andReturn(EXAMPLE_HASHCODE).anyTimes();
    replay(delegateCacheMock);
    RecordingProjectFileHashCache recordingLoader = RecordingProjectFileHashCache.createForCellRoot(delegateCacheMock, recordedFileHashes, new DistBuildConfig(FakeBuckConfig.builder().build()));
    recordingLoader.get(fs.relativize(pathDirA));
    assertThat(fileHashes.getEntries().size(), // all folders and files
    Matchers.equalTo(5));
    assertThat(fileHashes.getEntries(), IsCollectionContaining.hasItems(new FileHashEntryMatcher("a", true), new FileHashEntryMatcher("a/b", true), new FileHashEntryMatcher("a/b/c", false), new FileHashEntryMatcher("a/b/d", true), new FileHashEntryMatcher("a/e", false)));
}
Also used : ArchiveMemberPath(com.facebook.buck.io.ArchiveMemberPath) Path(java.nio.file.Path) BuildJobStateFileHashes(com.facebook.buck.distributed.thrift.BuildJobStateFileHashes) ProjectFileHashCache(com.facebook.buck.util.cache.ProjectFileHashCache) FakeProjectFileHashCache(com.facebook.buck.testutil.FakeProjectFileHashCache) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) FileHashEntryMatcher(com.facebook.buck.testutil.FileHashEntryMatcher) Test(org.junit.Test)

Example 7 with ProjectFileHashCache

use of com.facebook.buck.util.cache.ProjectFileHashCache in project buck by facebook.

the class DistBuildFileHashesTest method materializerWritesContents.

@Test
public void materializerWritesContents() throws Exception {
    SingleFileFixture f = new SingleFileFixture(tempDir);
    List<BuildJobStateFileHashes> fileHashes = f.distributedBuildFileHashes.getFileHashes();
    ProjectFilesystem materializeProjectFilesystem = new ProjectFilesystem(tempDir.newFolder("read_hashes").getCanonicalFile().toPath());
    ProjectFileHashCache mockCache = EasyMock.createMock(ProjectFileHashCache.class);
    EasyMock.expect(mockCache.getFilesystem()).andReturn(materializeProjectFilesystem).atLeastOnce();
    EasyMock.expect(mockCache.get(EasyMock.<Path>notNull())).andReturn(HashCode.fromInt(42)).once();
    EasyMock.replay(mockCache);
    MaterializerProjectFileHashCache materializer = new MaterializerProjectFileHashCache(mockCache, fileHashes.get(0), new InlineContentsProvider());
    materializer.get(materializeProjectFilesystem.resolve(f.javaSrcPath));
    assertThat(materializeProjectFilesystem.readFileIfItExists(f.javaSrcPath), Matchers.equalTo(Optional.of(f.writtenContents)));
}
Also used : ArchiveMemberSourcePath(com.facebook.buck.rules.ArchiveMemberSourcePath) ArchiveMemberPath(com.facebook.buck.io.ArchiveMemberPath) Path(java.nio.file.Path) PathSourcePath(com.facebook.buck.rules.PathSourcePath) SourcePath(com.facebook.buck.rules.SourcePath) BuildJobStateFileHashes(com.facebook.buck.distributed.thrift.BuildJobStateFileHashes) ProjectFileHashCache(com.facebook.buck.util.cache.ProjectFileHashCache) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) Test(org.junit.Test)

Example 8 with ProjectFileHashCache

use of com.facebook.buck.util.cache.ProjectFileHashCache in project buck by facebook.

the class CachingBuildEngine method verifyRecordedPathHashes.

private boolean verifyRecordedPathHashes(BuildTarget target, ProjectFilesystem filesystem, ImmutableMap<String, String> recordedPathHashes) throws IOException {
    // Create a new `DefaultFileHashCache` to prevent caching from interfering with verification.
    ProjectFileHashCache fileHashCache = DefaultFileHashCache.createDefaultFileHashCache(filesystem);
    // Verify each path from the recorded path hashes entry matches the actual on-disk version.
    for (Map.Entry<String, String> ent : recordedPathHashes.entrySet()) {
        Path path = filesystem.getPath(ent.getKey());
        HashCode cachedHashCode = HashCode.fromString(ent.getValue());
        HashCode realHashCode = fileHashCache.get(path);
        if (!realHashCode.equals(cachedHashCode)) {
            LOG.debug("%s: recorded hash for \"%s\" doesn't match actual hash: %s (cached) != %s (real).", target, path, cachedHashCode, realHashCode);
            return false;
        }
    }
    return true;
}
Also used : Path(java.nio.file.Path) LazyPath(com.facebook.buck.io.LazyPath) BorrowablePath(com.facebook.buck.io.BorrowablePath) HashCode(com.google.common.hash.HashCode) ProjectFileHashCache(com.facebook.buck.util.cache.ProjectFileHashCache) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap)

Example 9 with ProjectFileHashCache

use of com.facebook.buck.util.cache.ProjectFileHashCache in project buck by facebook.

the class MaterializerProjectFileHashCacheTest method testMaterializeDirectoryHelper.

private void testMaterializeDirectoryHelper(boolean materializeDuringPreloading, MaterializeFunction materializeFunction) throws IOException {
    // Scenario:
    // file hash entries for:
    // /a - folder
    // /a/b - folder
    // /a/b/c - file
    // /a/b/d - folder
    // /a/e - file
    // => preload: ensure all folders created and files touched
    // => materialize(/a): ensure all folders and sub-directories/files created
    assumeTrue(!Platform.detect().equals(Platform.WINDOWS));
    ProjectFilesystem projectFilesystem = new ProjectFilesystem(projectDir.getRoot().toPath());
    Path pathDirA = projectFilesystem.resolve("a");
    Path pathDirAb = projectFilesystem.resolve("a/b");
    Path pathFileAbc = projectFilesystem.resolve("a/b/c");
    Path pathDirAbd = projectFilesystem.resolve("a/b/d");
    Path pathFileAe = projectFilesystem.resolve("a/e");
    Path relativePathDirA = Paths.get("a");
    Path relativePathDirAb = Paths.get("a/b");
    Path relativePathFileAbc = Paths.get("a/b/c");
    Path relativePathDirAbd = Paths.get("a/b/d");
    Path relativePathFileAe = Paths.get("a/e");
    BuildJobStateFileHashes fileHashes = new BuildJobStateFileHashes();
    BuildJobStateFileHashEntry dirAFileHashEntry = new BuildJobStateFileHashEntry();
    dirAFileHashEntry.setPath(unixPath(relativePathDirA));
    dirAFileHashEntry.setHashCode(EXAMPLE_HASHCODE.toString());
    dirAFileHashEntry.setIsDirectory(true);
    dirAFileHashEntry.setChildren(ImmutableList.of(unixPath(relativePathDirAb), unixPath(relativePathFileAe)));
    dirAFileHashEntry.setMaterializeDuringPreloading(materializeDuringPreloading);
    fileHashes.addToEntries(dirAFileHashEntry);
    BuildJobStateFileHashEntry dirAbFileHashEntry = new BuildJobStateFileHashEntry();
    dirAbFileHashEntry.setPath(unixPath(relativePathDirAb));
    dirAbFileHashEntry.setHashCode(EXAMPLE_HASHCODE.toString());
    dirAbFileHashEntry.setIsDirectory(true);
    dirAbFileHashEntry.setChildren(ImmutableList.of(unixPath(relativePathFileAbc), unixPath(relativePathDirAbd)));
    dirAbFileHashEntry.setMaterializeDuringPreloading(materializeDuringPreloading);
    fileHashes.addToEntries(dirAbFileHashEntry);
    BuildJobStateFileHashEntry fileAbcFileHashEntry = new BuildJobStateFileHashEntry();
    fileAbcFileHashEntry.setPath(unixPath(relativePathFileAbc));
    fileAbcFileHashEntry.setHashCode(EXAMPLE_HASHCODE.toString());
    fileAbcFileHashEntry.setContents(FILE_CONTENTS.getBytes(StandardCharsets.UTF_8));
    fileAbcFileHashEntry.setIsDirectory(false);
    fileAbcFileHashEntry.setMaterializeDuringPreloading(materializeDuringPreloading);
    fileHashes.addToEntries(fileAbcFileHashEntry);
    BuildJobStateFileHashEntry dirAbdFileHashEntry = new BuildJobStateFileHashEntry();
    dirAbdFileHashEntry.setPath(unixPath(relativePathDirAbd));
    dirAbdFileHashEntry.setHashCode(EXAMPLE_HASHCODE.toString());
    dirAbdFileHashEntry.setIsDirectory(true);
    dirAbdFileHashEntry.setChildren(ImmutableList.of());
    dirAbdFileHashEntry.setMaterializeDuringPreloading(materializeDuringPreloading);
    fileHashes.addToEntries(dirAbdFileHashEntry);
    BuildJobStateFileHashEntry fileAeFileHashEntry = new BuildJobStateFileHashEntry();
    fileAeFileHashEntry.setPath(unixPath(relativePathFileAe));
    fileAeFileHashEntry.setHashCode(EXAMPLE_HASHCODE.toString());
    fileAeFileHashEntry.setContents(FILE_CONTENTS_TWO.getBytes(StandardCharsets.UTF_8));
    fileAeFileHashEntry.setIsDirectory(false);
    fileAeFileHashEntry.setMaterializeDuringPreloading(materializeDuringPreloading);
    fileHashes.addToEntries(fileAeFileHashEntry);
    InlineContentsProvider inlineProvider = new InlineContentsProvider();
    ProjectFileHashCache mockFileHashCache = EasyMock.createNiceMock(ProjectFileHashCache.class);
    expect(mockFileHashCache.getFilesystem()).andReturn(projectFilesystem).atLeastOnce();
    replay(mockFileHashCache);
    MaterializerProjectFileHashCache fileMaterializer = new MaterializerProjectFileHashCache(mockFileHashCache, fileHashes, inlineProvider);
    assertFalse(pathDirA.toFile().exists());
    assertFalse(pathDirAb.toFile().exists());
    assertFalse(pathFileAbc.toFile().exists());
    assertFalse(pathDirAbd.toFile().exists());
    assertFalse(pathFileAe.toFile().exists());
    materializeFunction.execute(fileMaterializer, pathDirA);
    assertTrue(pathDirA.toFile().exists());
    assertTrue(pathDirAb.toFile().exists());
    assertTrue(pathFileAbc.toFile().exists());
    assertTrue(pathDirAbd.toFile().exists());
    assertTrue(pathFileAe.toFile().exists());
}
Also used : Path(java.nio.file.Path) BuildJobStateFileHashEntry(com.facebook.buck.distributed.thrift.BuildJobStateFileHashEntry) BuildJobStateFileHashes(com.facebook.buck.distributed.thrift.BuildJobStateFileHashes) ProjectFileHashCache(com.facebook.buck.util.cache.ProjectFileHashCache) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem)

Example 10 with ProjectFileHashCache

use of com.facebook.buck.util.cache.ProjectFileHashCache in project buck by facebook.

the class MaterializerProjectFileHashCacheTest method testSymlinkToFileWithinExternalDirectory.

public void testSymlinkToFileWithinExternalDirectory(HashCode fileHashEntryHashCode, HashCode actualHashCode, MaterializeFunction materializeFunction, int expectCallsToGetHashMethod) throws IOException {
    // Scenario:
    //  path: /project/linktoexternaldir/externalfile
    //  symlink root: /project/linktoexternaldir -> /externalDir
    // => check that /project/linktoexternaldir/externalfile -> /externalDir/externalfile
    assumeTrue(!Platform.detect().equals(Platform.WINDOWS));
    ProjectFilesystem projectFilesystem = new ProjectFilesystem(projectDir.getRoot().toPath());
    File externalFile = externalDir.newFile("externalfile");
    Path symlinkRoot = projectFilesystem.resolve("linktoexternaldir");
    Path relativeSymlinkRoot = Paths.get("linktoexternaldir");
    // /project/linktoexternaldir/externalfile
    Path symlink = symlinkRoot.resolve("externalfile");
    Path relativeSymlink = projectFilesystem.getPathRelativeToProjectRoot(symlink).get();
    BuildJobStateFileHashEntry symlinkFileHashEntry = new BuildJobStateFileHashEntry();
    symlinkFileHashEntry.setRootSymLink(unixPath(relativeSymlinkRoot));
    symlinkFileHashEntry.setRootSymLinkTarget(unixPath(externalDir.getRoot().toPath()));
    symlinkFileHashEntry.setPath(unixPath(relativeSymlink));
    symlinkFileHashEntry.setHashCode(fileHashEntryHashCode.toString());
    BuildJobStateFileHashes fileHashes = new BuildJobStateFileHashes();
    fileHashes.addToEntries(symlinkFileHashEntry);
    FileContentsProvider mockFileProvider = EasyMock.createMock(FileContentsProvider.class);
    ProjectFileHashCache mockFileHashCache = EasyMock.createNiceMock(ProjectFileHashCache.class);
    expect(mockFileHashCache.getFilesystem()).andReturn(projectFilesystem).atLeastOnce();
    if (expectCallsToGetHashMethod > 0) {
        expect(mockFileHashCache.get(relativeSymlink)).andReturn(actualHashCode).times(expectCallsToGetHashMethod);
    }
    replay(mockFileHashCache);
    MaterializerProjectFileHashCache fileMaterializer = new MaterializerProjectFileHashCache(mockFileHashCache, fileHashes, mockFileProvider);
    assertFalse(symlink.toFile().exists());
    materializeFunction.execute(fileMaterializer, relativeSymlink);
    assertTrue(symlink.toFile().exists());
    assertThat(symlink.toRealPath(), Matchers.equalTo(externalFile.toPath().toRealPath()));
    verify(mockFileHashCache);
}
Also used : Path(java.nio.file.Path) BuildJobStateFileHashEntry(com.facebook.buck.distributed.thrift.BuildJobStateFileHashEntry) BuildJobStateFileHashes(com.facebook.buck.distributed.thrift.BuildJobStateFileHashes) ProjectFileHashCache(com.facebook.buck.util.cache.ProjectFileHashCache) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) File(java.io.File)

Aggregations

ProjectFileHashCache (com.facebook.buck.util.cache.ProjectFileHashCache)13 BuildJobStateFileHashes (com.facebook.buck.distributed.thrift.BuildJobStateFileHashes)9 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)9 Path (java.nio.file.Path)9 Test (org.junit.Test)4 BuildJobStateFileHashEntry (com.facebook.buck.distributed.thrift.BuildJobStateFileHashEntry)3 BroadcastEventListener (com.facebook.buck.event.listener.BroadcastEventListener)2 ArchiveMemberPath (com.facebook.buck.io.ArchiveMemberPath)2 Cell (com.facebook.buck.rules.Cell)2 StackedFileHashCache (com.facebook.buck.util.cache.StackedFileHashCache)2 ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 Map (java.util.Map)2 ConcurrentMap (java.util.concurrent.ConcurrentMap)2 AndroidBuckConfig (com.facebook.buck.android.AndroidBuckConfig)1 AndroidDirectoryResolver (com.facebook.buck.android.AndroidDirectoryResolver)1 AndroidPlatformTarget (com.facebook.buck.android.AndroidPlatformTarget)1 DefaultAndroidDirectoryResolver (com.facebook.buck.android.DefaultAndroidDirectoryResolver)1 NoAndroidSdkException (com.facebook.buck.android.NoAndroidSdkException)1 ArtifactCache (com.facebook.buck.artifact_cache.ArtifactCache)1