use of com.facebook.buck.artifact_cache.CacheResult in project buck by facebook.
the class ArtifactCacheHandler method handleGet.
private int handleGet(Request baseRequest, HttpServletResponse response) throws IOException {
if (!artifactCache.isPresent()) {
response.getWriter().write("Serving local cache is disabled for this instance.");
return HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
}
String path = baseRequest.getUri().getPath();
String[] pathElements = path.split("/");
if (pathElements.length != 4 || !pathElements[2].equals("key")) {
response.getWriter().write("Incorrect url format.");
return HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
}
RuleKey ruleKey = new RuleKey(pathElements[3]);
Path temp = null;
try {
projectFilesystem.mkdirs(projectFilesystem.getBuckPaths().getScratchDir());
temp = projectFilesystem.createTempFile(projectFilesystem.getBuckPaths().getScratchDir(), "outgoing_rulekey", ".tmp");
CacheResult fetchResult = artifactCache.get().fetch(ruleKey, LazyPath.ofInstance(temp));
if (!fetchResult.getType().isSuccess()) {
return HttpServletResponse.SC_NOT_FOUND;
}
final Path tempFinal = temp;
HttpArtifactCacheBinaryProtocol.FetchResponse fetchResponse = new HttpArtifactCacheBinaryProtocol.FetchResponse(ImmutableSet.of(ruleKey), fetchResult.getMetadata(), new ByteSource() {
@Override
public InputStream openStream() throws IOException {
return projectFilesystem.newFileInputStream(tempFinal);
}
});
fetchResponse.write(response.getOutputStream());
response.setContentLengthLong(fetchResponse.getContentLength());
return HttpServletResponse.SC_OK;
} finally {
if (temp != null) {
projectFilesystem.deleteFileAtPathIfExists(temp);
}
}
}
use of com.facebook.buck.artifact_cache.CacheResult in project buck by facebook.
the class CachingBuildEngine method processBuildRule.
private ListenableFuture<BuildResult> processBuildRule(final BuildRule rule, final BuildEngineBuildContext buildContext, final ExecutionContext executionContext, final OnDiskBuildInfo onDiskBuildInfo, final BuildInfoRecorder buildInfoRecorder, final BuildableContext buildableContext, final ConcurrentLinkedQueue<ListenableFuture<Void>> asyncCallbacks) {
// If we've already seen a failure, exit early.
if (!buildContext.isKeepGoing() && firstFailure != null) {
return Futures.immediateFuture(BuildResult.canceled(rule, firstFailure));
}
final RuleKeyFactories ruleKeyFactory = ruleKeyFactories.apply(rule.getProjectFilesystem());
try (BuildRuleEvent.Scope scope = BuildRuleEvent.resumeSuspendScope(buildContext.getEventBus(), rule, buildRuleDurationTracker, ruleKeyFactory.getDefaultRuleKeyFactory())) {
// 1. Check if it's already built.
Optional<RuleKey> cachedRuleKey = onDiskBuildInfo.getRuleKey(BuildInfo.MetadataKey.RULE_KEY);
final RuleKey defaultRuleKey = ruleKeyFactory.getDefaultRuleKeyFactory().build(rule);
if (defaultRuleKey.equals(cachedRuleKey.orElse(null))) {
return Futures.transform(markRuleAsUsed(rule, buildContext.getEventBus()), Functions.constant(BuildResult.success(rule, BuildRuleSuccessType.MATCHING_RULE_KEY, CacheResult.localKeyUnchangedHit())));
}
// 2. Rule key cache lookup.
ListenableFuture<CacheResult> rulekeyCacheResult = cacheActivityService.submit(() -> {
CacheResult cacheResult = tryToFetchArtifactFromBuildCacheAndOverlayOnTopOfProjectFilesystem(rule, defaultRuleKey, buildContext.getArtifactCache(), // TODO(shs96c): This should be a shared between all tests, not one per cell
rule.getProjectFilesystem(), buildContext);
if (cacheResult.getType().isSuccess()) {
fillMissingBuildMetadataFromCache(cacheResult, buildInfoRecorder, BuildInfo.MetadataKey.INPUT_BASED_RULE_KEY, BuildInfo.MetadataKey.DEP_FILE_RULE_KEY, BuildInfo.MetadataKey.DEP_FILE);
}
return cacheResult;
}, CACHE_CHECK_RESOURCE_AMOUNTS);
return Futures.transformAsync(rulekeyCacheResult, ruleAsyncFunction(rule, buildContext.getEventBus(), (cacheResult) -> handleRuleKeyCacheResult(rule, buildContext, executionContext, onDiskBuildInfo, buildInfoRecorder, buildableContext, asyncCallbacks, ruleKeyFactory, cacheResult)), serviceByAdjustingDefaultWeightsTo(SCHEDULING_MORE_WORK_RESOURCE_AMOUNTS));
}
}
use of com.facebook.buck.artifact_cache.CacheResult in project buck by facebook.
the class CachingBuildEngine method performInputBasedCacheFetch.
private Optional<BuildResult> performInputBasedCacheFetch(final BuildRule rule, final BuildEngineBuildContext context, final OnDiskBuildInfo onDiskBuildInfo, BuildInfoRecorder buildInfoRecorder, RuleKey inputRuleKey) {
Preconditions.checkArgument(SupportsInputBasedRuleKey.isSupported(rule));
buildInfoRecorder.addBuildMetadata(BuildInfo.MetadataKey.INPUT_BASED_RULE_KEY, inputRuleKey.toString());
// Check the input-based rule key says we're already built.
Optional<RuleKey> lastInputRuleKey = onDiskBuildInfo.getRuleKey(BuildInfo.MetadataKey.INPUT_BASED_RULE_KEY);
if (inputRuleKey.equals(lastInputRuleKey.orElse(null))) {
return Optional.of(BuildResult.success(rule, BuildRuleSuccessType.MATCHING_INPUT_BASED_RULE_KEY, CacheResult.localKeyUnchangedHit()));
}
// Try to fetch the artifact using the input-based rule key.
CacheResult cacheResult = tryToFetchArtifactFromBuildCacheAndOverlayOnTopOfProjectFilesystem(rule, inputRuleKey, context.getArtifactCache(), // TODO(shs96c): Share this 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_INPUT_BASED, cacheResult));
}
return Optional.empty();
}
use of com.facebook.buck.artifact_cache.CacheResult in project buck by facebook.
the class ServedCacheIntegrationTest method containsKey.
private boolean containsKey(ArtifactCache cache, RuleKey ruleKey) throws Exception {
Path fetchedContents = tmpDir.newFile();
CacheResult cacheResult = cache.fetch(ruleKey, LazyPath.ofInstance(fetchedContents));
assertThat(cacheResult.getType(), Matchers.oneOf(CacheResultType.HIT, CacheResultType.MISS));
return cacheResult.getType().isSuccess();
}
use of com.facebook.buck.artifact_cache.CacheResult in project buck by facebook.
the class ServedCacheIntegrationTest method testStoreAndFetchBorrowable.
@Test
public void testStoreAndFetchBorrowable() 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 = readwrite"), 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.borrowablePath(originalDataPath));
cacheResult = serverBackedCache.fetch(ruleKey, fetchedContents);
assertThat(cacheResult.getType().isSuccess(), Matchers.is(true));
assertThat(cacheResult.getMetadata(), Matchers.equalTo(metadata));
assertThat(projectFilesystem.readFileIfItExists(fetchedContents.get()).get(), Matchers.equalTo(data));
}
Aggregations