aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com
diff options
context:
space:
mode:
authorGravatar Justine Tunney <jart@google.com>2016-11-23 18:47:32 +0000
committerGravatar Klaus Aehlig <aehlig@google.com>2016-11-23 19:14:37 +0000
commit4c67807964e37cfd55bbcda4c6374fcc480bcecc (patch)
tree0b82ea75a1fca6ec5457cfbbd0304365699cfa52 /src/main/java/com
parentafe6051cf78c086cd9e4771a1a28782d3548a0af (diff)
Improve reliability/performance of Bazel downloads
1. We now retry on connection failures. a. With exponential backoff. b. While recovering quickly from ephemeral failures. c. While still working if internet or web server is slow. 2. We now request gzip responses from web server. Fixed #1760 Fixed #1910 RELNOTES: External downloads now retry with exponential backoff and support gzip content-encoding. -- MOS_MIGRATED_REVID=140049160
Diffstat (limited to 'src/main/java/com')
-rw-r--r--src/main/java/com/google/devtools/build/lib/bazel/repository/downloader/HttpConnection.java238
-rw-r--r--src/main/java/com/google/devtools/build/lib/bazel/repository/downloader/HttpDownloader.java15
-rw-r--r--src/main/java/com/google/devtools/build/lib/bazel/rules/workspace/HttpArchiveRule.java2
-rw-r--r--src/main/java/com/google/devtools/build/lib/bazel/rules/workspace/HttpFileRule.java3
-rw-r--r--src/main/java/com/google/devtools/build/lib/bazel/rules/workspace/HttpJarRule.java3
-rw-r--r--src/main/java/com/google/devtools/build/lib/bazel/rules/workspace/NewHttpArchiveRule.java2
6 files changed, 8 insertions, 255 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/bazel/repository/downloader/HttpConnection.java b/src/main/java/com/google/devtools/build/lib/bazel/repository/downloader/HttpConnection.java
deleted file mode 100644
index 84e6c47e63..0000000000
--- a/src/main/java/com/google/devtools/build/lib/bazel/repository/downloader/HttpConnection.java
+++ /dev/null
@@ -1,238 +0,0 @@
-// Copyright 2016 The Bazel Authors. All rights reserved.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-package com.google.devtools.build.lib.bazel.repository.downloader;
-
-import com.google.common.annotations.VisibleForTesting;
-import com.google.common.base.Optional;
-import com.google.common.io.ByteStreams;
-import com.google.common.net.MediaType;
-import java.io.Closeable;
-import java.io.IOException;
-import java.io.InputStream;
-import java.net.HttpURLConnection;
-import java.net.MalformedURLException;
-import java.net.Proxy;
-import java.net.SocketTimeoutException;
-import java.net.URL;
-import java.net.URLConnection;
-import java.nio.charset.Charset;
-import java.nio.charset.StandardCharsets;
-import java.util.Map;
-
-/**
- * Represents a connection over HTTP.
- */
-class HttpConnection implements Closeable {
- private static final int MAX_REDIRECTS = 20;
- private static final int TIMEOUT_MS = 60000;
- private final InputStream inputStream;
- private final int contentLength;
-
- private HttpConnection(InputStream inputStream, int contentLength) {
- this.inputStream = inputStream;
- this.contentLength = contentLength;
- }
-
- public InputStream getInputStream() {
- return inputStream;
- }
-
- /**
- * @return The length of the response, or -1 if unknown.
- */
- int getContentLength() {
- return contentLength;
- }
-
- @Override
- public void close() throws IOException {
- inputStream.close();
- }
-
- private static int parseContentLength(HttpURLConnection connection) {
- String length;
- try {
- length = connection.getHeaderField("Content-Length");
- if (length == null) {
- return -1;
- }
- return Integer.parseInt(length);
- } catch (NumberFormatException e) {
- return -1;
- }
- }
-
- /**
- * Connects to the given URL. Should not leave any connections open if anything goes wrong.
- */
- static HttpConnection createAndConnect(URL url, Map<String, String> clientEnv)
- throws IOException {
- Proxy proxy = ProxyHelper.createProxyIfNeeded(url.toString(), clientEnv);
- for (int i = 0; i < MAX_REDIRECTS; ++i) {
- URLConnection urlConnection = url.openConnection(proxy);
- if (!(urlConnection instanceof HttpURLConnection)) {
- return createFileConnection(urlConnection);
- }
-
- HttpURLConnection connection = (HttpURLConnection) urlConnection;
- int statusCode;
- try {
- statusCode = createAndConnectViaHttp(connection);
- } catch (IOException e) {
- connection.disconnect();
- throw e;
- }
-
- switch (statusCode) {
- case HttpURLConnection.HTTP_OK:
- try {
- return new HttpConnection(connection.getInputStream(), parseContentLength(connection));
- } catch (IOException e) {
- connection.disconnect();
- throw e;
- }
- case HttpURLConnection.HTTP_MOVED_PERM:
- case HttpURLConnection.HTTP_MOVED_TEMP:
- // Try again with the new URL. This is the only case that doesn't return/throw.
- url = tryGetLocation(statusCode, connection);
- connection.disconnect();
- break;
- case -1:
- throw new IOException("An HTTP error occurred");
- default:
- throw new IOException(
- String.format(
- "%s %s: %s",
- connection.getResponseCode(),
- connection.getResponseMessage(),
- readBody(connection)));
- }
- }
- throw new IOException("Maximum redirects (" + MAX_REDIRECTS + ") exceeded");
- }
-
- // For file:// URLs.
- private static HttpConnection createFileConnection(URLConnection connection)
- throws IOException {
- int contentLength = connection.getContentLength();
- // check for empty file. -1 is a valid contentLength, meaning the size of unknown. It's a
- // common return value for an FTP download request for example. Local files will always
- // have a valid contentLength value.
- if (contentLength == 0) {
- throw new IOException("Attempted to download an empty file");
- }
-
- return new HttpConnection(connection.getInputStream(), contentLength);
- }
-
- private static int createAndConnectViaHttp(HttpURLConnection connection) throws IOException {
- connection.setConnectTimeout(TIMEOUT_MS);
- connection.setReadTimeout(TIMEOUT_MS);
- try {
- connection.connect();
- } catch (SocketTimeoutException e) {
- throw new IOException(
- "Timed out connecting to " + connection.getURL() + " : " + e.getMessage(), e);
- } catch (IllegalArgumentException | IOException e) {
- throw new IOException(
- "Failed to connect to " + connection.getURL() + " : " + e.getMessage(), e);
- }
- return connection.getResponseCode();
- }
-
- private static URL tryGetLocation(int statusCode, HttpURLConnection connection)
- throws IOException {
- String newLocation = connection.getHeaderField("Location");
- if (newLocation == null) {
- throw new IOException(
- "Remote returned " + statusCode + " but did not return location header.");
- }
-
- URL newUrl;
- try {
- newUrl = new URL(newLocation);
- } catch (MalformedURLException e) {
- throw new IOException("Remote returned invalid location header: " + newLocation);
- }
-
- String newProtocol = newUrl.getProtocol();
- if (!("http".equals(newProtocol) || "https".equals(newProtocol))) {
- throw new IOException(
- "Remote returned invalid location header: " + newLocation);
- }
-
- return newUrl;
- }
-
- /**
- * Attempts to detect the encoding the HTTP reponse is using.
- *
- * <p>This attempts to read the Content-Encoding header, then the Content-Type header,
- * then just falls back to UTF-8.</p>
- *
- * @throws IOException If something goes wrong (the encoding isn't parsable or is, but isn't
- * supported by the system).
- */
- @VisibleForTesting
- static Charset getEncoding(HttpURLConnection connection) throws IOException {
- String encoding = connection.getContentEncoding();
- if (encoding != null) {
- if (Charset.availableCharsets().containsKey(encoding)) {
- try {
- return Charset.forName(encoding);
- } catch (IllegalArgumentException | UnsupportedOperationException e) {
- throw new IOException(
- "Got invalid encoding from " + connection.getURL() + ": " + encoding);
- }
- } else {
- throw new IOException(
- "Got unavailable encoding from " + connection.getURL() + ": " + encoding);
- }
- }
- encoding = connection.getContentType();
- if (encoding == null) {
- return StandardCharsets.UTF_8;
- }
- try {
- MediaType mediaType = MediaType.parse(encoding);
- if (mediaType == null) {
- return StandardCharsets.UTF_8;
- }
- Optional<Charset> charset = mediaType.charset();
- if (charset.isPresent()) {
- return charset.get();
- }
- } catch (IllegalArgumentException | IllegalStateException e) {
- throw new IOException(
- "Got invalid encoding from " + connection.getURL() + ": " + encoding);
- }
- return StandardCharsets.UTF_8;
- }
-
- private static String readBody(HttpURLConnection connection) throws IOException {
- InputStream errorStream = connection.getErrorStream();
- Charset encoding = getEncoding(connection);
- if (errorStream != null) {
- return new String(ByteStreams.toByteArray(errorStream), encoding);
- }
-
- InputStream responseStream = connection.getInputStream();
- if (responseStream != null) {
- return new String(ByteStreams.toByteArray(responseStream), encoding);
- }
-
- return null;
- }
-}
diff --git a/src/main/java/com/google/devtools/build/lib/bazel/repository/downloader/HttpDownloader.java b/src/main/java/com/google/devtools/build/lib/bazel/repository/downloader/HttpDownloader.java
index 400f9e2d02..ea4cc81d7c 100644
--- a/src/main/java/com/google/devtools/build/lib/bazel/repository/downloader/HttpDownloader.java
+++ b/src/main/java/com/google/devtools/build/lib/bazel/repository/downloader/HttpDownloader.java
@@ -31,6 +31,7 @@ import com.google.devtools.build.skyframe.SkyFunctionException.Transience;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
+import java.net.Proxy;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
@@ -82,10 +83,7 @@ public class HttpDownloader {
try {
return download(url, sha256, type, outputDirectory, eventHandler, clientEnv);
} catch (IOException e) {
- throw new RepositoryFunctionException(
- new IOException(
- "Error downloading from " + url + " to " + outputDirectory + ": " + e.getMessage()),
- SkyFunctionException.Transience.TRANSIENT);
+ throw new RepositoryFunctionException(e, SkyFunctionException.Transience.TRANSIENT);
}
}
@@ -130,10 +128,10 @@ public class HttpDownloader {
AtomicInteger totalBytes = new AtomicInteger(0);
final ScheduledFuture<?> loggerHandle = getLoggerHandle(totalBytes, eventHandler, urlString);
final URL url = new URL(urlString);
+ Proxy proxy = ProxyHelper.createProxyIfNeeded(url.toString(), clientEnv);
try (OutputStream out = destination.getOutputStream();
- HttpConnection connection = HttpConnection.createAndConnect(url, clientEnv)) {
- InputStream inputStream = connection.getInputStream();
+ InputStream inputStream = HttpConnector.connect(url, proxy, eventHandler)) {
int read;
byte[] buf = new byte[BUFFER_SIZE];
while ((read = inputStream.read(buf)) > 0) {
@@ -143,11 +141,6 @@ public class HttpDownloader {
throw new InterruptedException("Download interrupted");
}
}
- if (connection.getContentLength() != -1
- && totalBytes.get() != connection.getContentLength()) {
- throw new IOException("Expected " + formatSize(connection.getContentLength()) + ", got "
- + formatSize(totalBytes.get()));
- }
} catch (IOException e) {
throw new IOException(
"Error downloading " + url + " to " + destination + ": " + e.getMessage());
diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/workspace/HttpArchiveRule.java b/src/main/java/com/google/devtools/build/lib/bazel/rules/workspace/HttpArchiveRule.java
index bc5a9cdf20..0406fabf8e 100644
--- a/src/main/java/com/google/devtools/build/lib/bazel/rules/workspace/HttpArchiveRule.java
+++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/workspace/HttpArchiveRule.java
@@ -39,7 +39,7 @@ public class HttpArchiveRule implements RuleDefinition {
A URL referencing an archive file containing a Bazel repository.
<p>Archives of type .zip, .jar, .war, .tar.gz or .tgz are supported. There is no support
- for authentication. Redirections are followed, but not from HTTP to HTTPS.</p>
+ for authentication.</p>
<!-- #END_BLAZE_RULE.ATTRIBUTE --> */
.add(attr("url", STRING).mandatory())
/* <!-- #BLAZE_RULE(http_archive).ATTRIBUTE(sha256) -->
diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/workspace/HttpFileRule.java b/src/main/java/com/google/devtools/build/lib/bazel/rules/workspace/HttpFileRule.java
index 97a8dc3ac5..7c96c4cc32 100644
--- a/src/main/java/com/google/devtools/build/lib/bazel/rules/workspace/HttpFileRule.java
+++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/workspace/HttpFileRule.java
@@ -39,8 +39,7 @@ public class HttpFileRule implements RuleDefinition {
/* <!-- #BLAZE_RULE(http_file).ATTRIBUTE(url) -->
A URL to a file that will be made available to Bazel.
- <p>This must be an http or https URL. Authentication is not support.
- Redirections are followed, but not from HTTP to HTTPS.</p>
+ <p>This must be an http or https URL. Authentication is not supported.</p>
<!-- #END_BLAZE_RULE.ATTRIBUTE --> */
.add(attr("url", STRING).mandatory())
/* <!-- #BLAZE_RULE(http_file).ATTRIBUTE(sha256) -->
diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/workspace/HttpJarRule.java b/src/main/java/com/google/devtools/build/lib/bazel/rules/workspace/HttpJarRule.java
index 82a49ef583..14c3b16177 100644
--- a/src/main/java/com/google/devtools/build/lib/bazel/rules/workspace/HttpJarRule.java
+++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/workspace/HttpJarRule.java
@@ -38,8 +38,7 @@ public class HttpJarRule implements RuleDefinition {
/* <!-- #BLAZE_RULE(http_jar).ATTRIBUTE(url) -->
A URL to an archive file containing a Bazel repository.
- <p>This must be an http or https URL that ends with .jar. Redirections are followed, but
- not from HTTP to HTTPS.</p>
+ <p>This must be an http or https URL that ends with .jar.</p>
<!-- #END_BLAZE_RULE.ATTRIBUTE --> */
.add(attr("url", STRING).mandatory())
/* <!-- #BLAZE_RULE(http_jar).ATTRIBUTE(sha256) -->
diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/workspace/NewHttpArchiveRule.java b/src/main/java/com/google/devtools/build/lib/bazel/rules/workspace/NewHttpArchiveRule.java
index 70ddb28e21..30a5cd8b41 100644
--- a/src/main/java/com/google/devtools/build/lib/bazel/rules/workspace/NewHttpArchiveRule.java
+++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/workspace/NewHttpArchiveRule.java
@@ -36,7 +36,7 @@ public class NewHttpArchiveRule implements RuleDefinition {
A URL referencing an archive file containing a Bazel repository.
<p>Archives of type .zip, .jar, .war, .tar.gz or .tgz are supported. There is no support
- for authentication. Redirections are followed, but not from HTTP to HTTPS.</p>
+ for authentication.</p>
<!-- #END_BLAZE_RULE.ATTRIBUTE --> */
.add(attr("url", STRING).mandatory())
/* <!-- #BLAZE_RULE(new_http_archive).ATTRIBUTE(sha256) -->