Search in sources :

Example 16 with LazyPath

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());
        }
    }
}
Also used : LazyPath(com.facebook.buck.io.LazyPath) Path(java.nio.file.Path) OutputStream(java.io.OutputStream) Request(okhttp3.Request) HttpResponse(com.facebook.buck.slb.HttpResponse) DataInputStream(java.io.DataInputStream)

Example 17 with LazyPath

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();
    }
}
Also used : ArtifactInfo(com.facebook.buck.artifact_cache.ArtifactInfo) ArtifactCacheBuckConfig(com.facebook.buck.artifact_cache.ArtifactCacheBuckConfig) RuleKey(com.facebook.buck.rules.RuleKey) BorrowablePath(com.facebook.buck.io.BorrowablePath) LazyPath(com.facebook.buck.io.LazyPath) IOException(java.io.IOException) ArtifactCache(com.facebook.buck.artifact_cache.ArtifactCache) Test(org.junit.Test)

Example 18 with LazyPath

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));
}
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 19 with LazyPath

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

LazyPath (com.facebook.buck.io.LazyPath)19 Test (org.junit.Test)12 Path (java.nio.file.Path)10 BorrowablePath (com.facebook.buck.io.BorrowablePath)9 ArtifactCache (com.facebook.buck.artifact_cache.ArtifactCache)8 CacheResult (com.facebook.buck.artifact_cache.CacheResult)8 RuleKey (com.facebook.buck.rules.RuleKey)6 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)5 OutputStream (java.io.OutputStream)3 HttpResponse (com.facebook.buck.slb.HttpResponse)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 Request (okhttp3.Request)2 ArtifactCacheBuckConfig (com.facebook.buck.artifact_cache.ArtifactCacheBuckConfig)1 ArtifactInfo (com.facebook.buck.artifact_cache.ArtifactInfo)1 ArtifactMetadata (com.facebook.buck.artifact_cache.thrift.ArtifactMetadata)1 BuckCacheFetchRequest (com.facebook.buck.artifact_cache.thrift.BuckCacheFetchRequest)1 BuckCacheFetchResponse (com.facebook.buck.artifact_cache.thrift.BuckCacheFetchResponse)1 BuckCacheRequest (com.facebook.buck.artifact_cache.thrift.BuckCacheRequest)1 BuckCacheResponse (com.facebook.buck.artifact_cache.thrift.BuckCacheResponse)1