use of com.facebook.buck.io.LazyPath in project buck by facebook.
the class HttpArtifactCache method fetchImpl.
@Override
protected CacheResult fetchImpl(RuleKey ruleKey, LazyPath output, final Finished.Builder eventBuilder) throws IOException {
Request.Builder requestBuilder = new Request.Builder().get();
try (HttpResponse response = fetchClient.makeRequest("/artifacts/key/" + ruleKey.toString(), requestBuilder)) {
eventBuilder.getFetchBuilder().setResponseSizeBytes(response.contentLength());
try (DataInputStream input = new DataInputStream(new FullyReadOnCloseInputStream(response.getBody()))) {
if (response.statusCode() == HttpURLConnection.HTTP_NOT_FOUND) {
LOG.info("fetch(%s, %s): cache miss", response.requestUrl(), ruleKey);
return CacheResult.miss();
}
if (response.statusCode() != HttpURLConnection.HTTP_OK) {
String msg = String.format("unexpected server response: [%d:%s]", response.statusCode(), response.statusMessage());
reportFailure("fetch(%s, %s): %s", response.requestUrl(), ruleKey, msg);
eventBuilder.getFetchBuilder().setErrorMessage(msg);
return CacheResult.error(name, msg);
}
// Setup a temporary file, which sits next to the destination, to write to and
// make sure all parent dirs exist.
Path file = output.get();
projectFilesystem.createParentDirs(file);
Path temp = projectFilesystem.createTempFile(file.getParent(), file.getFileName().toString(), ".tmp");
FetchResponseReadResult fetchedData;
try (OutputStream tempFileOutputStream = projectFilesystem.newFileOutputStream(temp)) {
fetchedData = HttpArtifactCacheBinaryProtocol.readFetchResponse(input, tempFileOutputStream);
}
eventBuilder.setTarget(ArtifactCacheEvent.getTarget(fetchedData.getMetadata())).getFetchBuilder().setResponseSizeBytes(fetchedData.getResponseSizeBytes()).setArtifactContentHash(fetchedData.getArtifactOnlyHashCode().toString());
// Verify that we were one of the rule keys that stored this artifact.
if (!fetchedData.getRuleKeys().contains(ruleKey)) {
String msg = "incorrect key name";
reportFailure("fetch(%s, %s): %s", response.requestUrl(), ruleKey, msg);
eventBuilder.getFetchBuilder().setErrorMessage(msg);
return CacheResult.error(name, msg);
}
// the HTTP header. If it's incorrect, log this and return a miss.
if (!fetchedData.getExpectedHashCode().equals(fetchedData.getActualHashCode())) {
String msg = "artifact had invalid checksum";
reportFailure("fetch(%s, %s): %s", response.requestUrl(), ruleKey, msg);
projectFilesystem.deleteFileAtPath(temp);
eventBuilder.getFetchBuilder().setErrorMessage(msg);
return CacheResult.error(name, msg);
}
// Finally, move the temp file into it's final place.
projectFilesystem.move(temp, file, StandardCopyOption.REPLACE_EXISTING);
LOG.info("fetch(%s, %s): cache hit", response.requestUrl(), ruleKey);
return CacheResult.hit(name, fetchedData.getMetadata(), fetchedData.getResponseSizeBytes());
}
}
}
use of com.facebook.buck.io.LazyPath in project buck by facebook.
the class ServedCacheIntegrationTest method testMultipleNamedCaches.
@Test
public void testMultipleNamedCaches() throws Exception {
LazyPath fetchedContents = LazyPath.ofInstance(tmpDir.newFile());
final RuleKey bFileRuleKey = new RuleKey("baadbeef");
webServer = new WebServer(/* port */
0, projectFilesystem, "/static/", MAPPER);
webServer.updateAndStartIfNeeded(Optional.of(dirCache));
ArtifactCache secondCache = new ArtifactCache() {
@Override
public CacheResult fetch(RuleKey ruleKey, LazyPath output) {
if (ruleKey.equals(bFileRuleKey)) {
try {
projectFilesystem.writeContentsToPath("second", output.get());
} catch (IOException e) {
throw new RuntimeException(e);
}
return CacheResult.hit("secondCache");
}
return CacheResult.miss();
}
@Override
public ListenableFuture<Void> store(ArtifactInfo info, BorrowablePath output) {
return Futures.immediateFuture(null);
}
@Override
public boolean isStoreSupported() {
return true;
}
@Override
public void close() {
// Intentional no-op.
}
};
assertThat(secondCache.fetch(A_FILE_RULE_KEY, fetchedContents).getType(), Matchers.equalTo(CacheResultType.MISS));
assertThat(secondCache.fetch(bFileRuleKey, fetchedContents).getType(), Matchers.equalTo(CacheResultType.HIT));
WebServer secondWebServer = new WebServer(/* port */
0, projectFilesystem, "/static/", MAPPER);
try {
secondWebServer.updateAndStartIfNeeded(Optional.of(secondCache));
ArtifactCacheBuckConfig mutltiCacheConfig = createMockLocalConfig("[cache]", "mode = http", "http_cache_names = one, two", "[cache#two]", String.format("http_url = http://127.0.0.1:%d/", secondWebServer.getPort().get()), "[cache#one]", String.format("http_url = http://127.0.0.1:%d/", webServer.getPort().get()));
ArtifactCache serverBackedCache = createArtifactCache(mutltiCacheConfig);
assertThat(serverBackedCache.fetch(A_FILE_RULE_KEY, fetchedContents).getType(), Matchers.equalTo(CacheResultType.HIT));
assertThat(serverBackedCache.fetch(bFileRuleKey, fetchedContents).getType(), Matchers.equalTo(CacheResultType.HIT));
} finally {
secondWebServer.stop();
}
}
use of com.facebook.buck.io.LazyPath 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.io.LazyPath 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