Search in sources :

Example 11 with CacheResult

use of com.facebook.buck.artifact_cache.CacheResult in project buck by facebook.

the class CachingBuildEngine method tryToFetchArtifactFromBuildCacheAndOverlayOnTopOfProjectFilesystem.

private CacheResult tryToFetchArtifactFromBuildCacheAndOverlayOnTopOfProjectFilesystem(final BuildRule rule, final RuleKey ruleKey, final ArtifactCache artifactCache, final ProjectFilesystem filesystem, final BuildEngineBuildContext buildContext) {
    if (!rule.isCacheable()) {
        return CacheResult.ignored();
    }
    // Create a temp file whose extension must be ".zip" for Filesystems.newFileSystem() to infer
    // that we are creating a zip-based FileSystem.
    final LazyPath lazyZipPath = new LazyPath() {

        @Override
        protected Path create() throws IOException {
            return Files.createTempFile("buck_artifact_" + MoreFiles.sanitize(rule.getBuildTarget().getShortName()), ".zip");
        }
    };
    // TODO(bolinfest): Change ArtifactCache.fetch() so that it returns a File instead of takes one.
    // Then we could download directly from the remote cache into the on-disk cache and unzip it
    // from there.
    CacheResult cacheResult = fetchArtifactForBuildable(ruleKey, lazyZipPath, artifactCache);
    return unzipArtifactFromCacheResult(rule, ruleKey, lazyZipPath, buildContext, filesystem, cacheResult);
}
Also used : CacheResult(com.facebook.buck.artifact_cache.CacheResult) LazyPath(com.facebook.buck.io.LazyPath)

Example 12 with CacheResult

use of com.facebook.buck.artifact_cache.CacheResult in project buck by facebook.

the class CachingBuildEngine method performManifestBasedCacheFetch.

// Fetch an artifact from the cache using manifest-based caching.
private Optional<BuildResult> performManifestBasedCacheFetch(final BuildRule rule, final BuildEngineBuildContext context, BuildInfoRecorder buildInfoRecorder, RuleKeyAndInputs manifestKey) throws IOException {
    Preconditions.checkArgument(useManifestCaching(rule));
    final LazyPath tempFile = new LazyPath() {

        @Override
        protected Path create() throws IOException {
            return Files.createTempFile("buck.", ".manifest");
        }
    };
    CacheResult manifestResult = fetchArtifactForBuildable(manifestKey.getRuleKey(), tempFile, context.getArtifactCache());
    if (!manifestResult.getType().isSuccess()) {
        return Optional.empty();
    }
    Path manifestPath = getManifestPath(rule);
    // Clear out any existing manifest.
    rule.getProjectFilesystem().deleteFileAtPathIfExists(manifestPath);
    // Now, fetch an existing manifest from the cache.
    rule.getProjectFilesystem().createParentDirs(manifestPath);
    try (OutputStream outputStream = rule.getProjectFilesystem().newFileOutputStream(manifestPath);
        InputStream inputStream = new GZIPInputStream(new BufferedInputStream(Files.newInputStream(tempFile.get())))) {
        ByteStreams.copy(inputStream, outputStream);
    }
    Files.delete(tempFile.get());
    // Deserialize the manifest.
    Manifest manifest;
    try (InputStream input = rule.getProjectFilesystem().newFileInputStream(manifestPath)) {
        manifest = new Manifest(input);
    }
    // Lookup the rule for the current state of our inputs.
    Optional<RuleKey> ruleKey = manifest.lookup(fileHashCache, pathResolver, manifestKey.getInputs());
    if (!ruleKey.isPresent()) {
        return Optional.empty();
    }
    CacheResult cacheResult = tryToFetchArtifactFromBuildCacheAndOverlayOnTopOfProjectFilesystem(rule, ruleKey.get(), context.getArtifactCache(), // TODO(shs96c): This should be shared between all tests, not one per cell
    rule.getProjectFilesystem(), context);
    if (cacheResult.getType().isSuccess()) {
        fillMissingBuildMetadataFromCache(cacheResult, buildInfoRecorder, BuildInfo.MetadataKey.DEP_FILE_RULE_KEY, BuildInfo.MetadataKey.DEP_FILE);
        return Optional.of(BuildResult.success(rule, BuildRuleSuccessType.FETCHED_FROM_CACHE_MANIFEST_BASED, cacheResult));
    }
    return Optional.empty();
}
Also used : Path(java.nio.file.Path) LazyPath(com.facebook.buck.io.LazyPath) BorrowablePath(com.facebook.buck.io.BorrowablePath) GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) SupportsInputBasedRuleKey(com.facebook.buck.rules.keys.SupportsInputBasedRuleKey) SupportsDependencyFileRuleKey(com.facebook.buck.rules.keys.SupportsDependencyFileRuleKey) GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) CacheResult(com.facebook.buck.artifact_cache.CacheResult) LazyPath(com.facebook.buck.io.LazyPath)

Example 13 with CacheResult

use of com.facebook.buck.artifact_cache.CacheResult in project buck by facebook.

the class LocalFsContentsProvider method materializeFileContents.

@Override
public boolean materializeFileContents(BuildJobStateFileHashEntry entry, Path targetAbsPath) throws IOException {
    RuleKey key = new RuleKey(entry.getHashCode());
    CacheResult cacheResult = dirCache.fetch(key, LazyPath.ofInstance(targetAbsPath));
    return cacheResult.getType() == CacheResultType.HIT;
}
Also used : RuleKey(com.facebook.buck.rules.RuleKey) CacheResult(com.facebook.buck.artifact_cache.CacheResult)

Example 14 with CacheResult

use of com.facebook.buck.artifact_cache.CacheResult in project buck by facebook.

the class ServedCacheIntegrationTest method testStoreDisabled.

@Test
public void testStoreDisabled() throws Exception {
    webServer = new WebServer(/* port */
    0, projectFilesystem, "/static/", MAPPER);
    webServer.updateAndStartIfNeeded(ArtifactCaches.newServedCache(createMockLocalConfig("[cache]", "dir = test-cache", "serve_local_cache = true", "served_local_cache_mode = readonly"), projectFilesystem));
    ArtifactCache serverBackedCache = createArtifactCache(createMockLocalHttpCacheConfig(webServer.getPort().get()));
    RuleKey ruleKey = new RuleKey("00111222333444");
    ImmutableMap<String, String> metadata = ImmutableMap.of("some key", "some value");
    Path originalDataPath = tmpDir.newFile();
    String data = "you won't believe this!";
    projectFilesystem.writeContentsToPath(data, originalDataPath);
    LazyPath fetchedContents = LazyPath.ofInstance(tmpDir.newFile());
    CacheResult cacheResult = serverBackedCache.fetch(ruleKey, fetchedContents);
    assertThat(cacheResult.getType().isSuccess(), Matchers.is(false));
    serverBackedCache.store(ArtifactInfo.builder().addRuleKeys(ruleKey).setMetadata(metadata).build(), BorrowablePath.notBorrowablePath(originalDataPath));
    cacheResult = serverBackedCache.fetch(ruleKey, fetchedContents);
    assertThat(cacheResult.getType().isSuccess(), Matchers.is(false));
}
Also used : Path(java.nio.file.Path) BorrowablePath(com.facebook.buck.io.BorrowablePath) LazyPath(com.facebook.buck.io.LazyPath) RuleKey(com.facebook.buck.rules.RuleKey) CacheResult(com.facebook.buck.artifact_cache.CacheResult) LazyPath(com.facebook.buck.io.LazyPath) ArtifactCache(com.facebook.buck.artifact_cache.ArtifactCache) Test(org.junit.Test)

Example 15 with CacheResult

use of com.facebook.buck.artifact_cache.CacheResult in project buck by facebook.

the class ServedCacheIntegrationTest method testMalformedDirCacheMetaData.

@Test
public void testMalformedDirCacheMetaData() throws Exception {
    ArtifactCache cache = TestArtifactCaches.createDirCacheForTest(projectFilesystem.getRootPath(), Paths.get("test-cache"));
    Path cacheFilePath = DirArtifactCacheTestUtil.getPathForRuleKey(cache, A_FILE_RULE_KEY, Optional.of(".metadata"));
    assertThat(projectFilesystem.exists(cacheFilePath), Matchers.is(true));
    try (DataOutputStream outputStream = new DataOutputStream(projectFilesystem.newFileOutputStream(cacheFilePath))) {
        outputStream.writeInt(1024);
    }
    webServer = new WebServer(/* port */
    0, projectFilesystem, "/static/", MAPPER);
    webServer.updateAndStartIfNeeded(Optional.of(dirCache));
    ArtifactCache serverBackedCache = createArtifactCache(createMockLocalHttpCacheConfig(webServer.getPort().get()));
    LazyPath fetchedContents = LazyPath.ofInstance(tmpDir.newFile());
    CacheResult cacheResult = serverBackedCache.fetch(A_FILE_RULE_KEY, fetchedContents);
    assertThat(cacheResult.getType(), Matchers.equalTo(CacheResultType.MISS));
}
Also used : Path(java.nio.file.Path) BorrowablePath(com.facebook.buck.io.BorrowablePath) LazyPath(com.facebook.buck.io.LazyPath) DataOutputStream(java.io.DataOutputStream) CacheResult(com.facebook.buck.artifact_cache.CacheResult) LazyPath(com.facebook.buck.io.LazyPath) ArtifactCache(com.facebook.buck.artifact_cache.ArtifactCache) Test(org.junit.Test)

Aggregations

CacheResult (com.facebook.buck.artifact_cache.CacheResult)15 LazyPath (com.facebook.buck.io.LazyPath)12 BorrowablePath (com.facebook.buck.io.BorrowablePath)10 Path (java.nio.file.Path)10 ArtifactCache (com.facebook.buck.artifact_cache.ArtifactCache)8 Test (org.junit.Test)7 RuleKey (com.facebook.buck.rules.RuleKey)5 InputStream (java.io.InputStream)4 SupportsDependencyFileRuleKey (com.facebook.buck.rules.keys.SupportsDependencyFileRuleKey)3 SupportsInputBasedRuleKey (com.facebook.buck.rules.keys.SupportsInputBasedRuleKey)3 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)2 BuildTarget (com.facebook.buck.model.BuildTarget)2 IOException (java.io.IOException)2 ArtifactInfo (com.facebook.buck.artifact_cache.ArtifactInfo)1 CacheResultType (com.facebook.buck.artifact_cache.CacheResultType)1 HttpArtifactCacheBinaryProtocol (com.facebook.buck.artifact_cache.HttpArtifactCacheBinaryProtocol)1 ArtifactCompressionEvent (com.facebook.buck.event.ArtifactCompressionEvent)1 BuckEvent (com.facebook.buck.event.BuckEvent)1 BuckEventBus (com.facebook.buck.event.BuckEventBus)1 ConsoleEvent (com.facebook.buck.event.ConsoleEvent)1