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);
}
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();
}
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;
}
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));
}
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));
}
Aggregations