Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[7.5.0] Also apply --experimental_repository_downloader_retries to a SocketException #24722

Merged
merged 1 commit into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import com.google.devtools.build.lib.vfs.PathFragment;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.SocketException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
Expand Down Expand Up @@ -357,19 +358,23 @@ private boolean shouldRetryDownload(IOException e, int attempt) {
return false;
}

if (e instanceof ContentLengthMismatchException) {
if (isRetryableException(e)) {
return true;
}

for (var suppressed : e.getSuppressed()) {
if (suppressed instanceof ContentLengthMismatchException) {
if (isRetryableException(suppressed)) {
return true;
}
}

return false;
}

private boolean isRetryableException(Throwable e) {
return e instanceof ContentLengthMismatchException || e instanceof SocketException;
}

/**
* Downloads the contents of one URL and reads it into a byte array.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.URI;
import java.net.URL;
Expand Down Expand Up @@ -774,6 +775,99 @@ public void download_contentLengthMismatchWithOtherErrors_retries() throws Excep
assertThat(content).isEqualTo("content");
}

@Test
public void download_socketException_retries() throws Exception {
Downloader downloader = mock(Downloader.class);
HttpDownloader httpDownloader = mock(HttpDownloader.class);
int retries = 5;
DownloadManager downloadManager =
new DownloadManager(repositoryCache, downloader, httpDownloader);
downloadManager.setRetries(retries);
AtomicInteger times = new AtomicInteger(0);
byte[] data = "content".getBytes(UTF_8);
doAnswer(
(Answer<Void>)
invocationOnMock -> {
if (times.getAndIncrement() < 3) {
throw new SocketException("Connection reset");
}
Path output = invocationOnMock.getArgument(5, Path.class);
try (OutputStream outputStream = output.getOutputStream()) {
ByteStreams.copy(new ByteArrayInputStream(data), outputStream);
}

return null;
})
.when(downloader)
.download(any(), any(), any(), any(), any(), any(), any(), any(), any());

Path result =
download(
downloadManager,
ImmutableList.of(new URL("http://localhost")),
ImmutableMap.of(),
ImmutableMap.of(),
Optional.empty(),
"testCanonicalId",
Optional.empty(),
fs.getPath(workingDir.newFile().getAbsolutePath()),
eventHandler,
ImmutableMap.of(),
"testRepo");

assertThat(times.get()).isEqualTo(4);
String content = new String(ByteStreams.toByteArray(result.getInputStream()), UTF_8);
assertThat(content).isEqualTo("content");
}

@Test
public void download_socketExceptionWithOtherErrors_retries() throws Exception {
Downloader downloader = mock(Downloader.class);
HttpDownloader httpDownloader = mock(HttpDownloader.class);
int retries = 5;
DownloadManager downloadManager =
new DownloadManager(repositoryCache, downloader, httpDownloader);
downloadManager.setRetries(retries);
AtomicInteger times = new AtomicInteger(0);
byte[] data = "content".getBytes(UTF_8);
doAnswer(
(Answer<Void>)
invocationOnMock -> {
if (times.getAndIncrement() < 3) {
IOException e = new IOException();
e.addSuppressed(new SocketException("Connection reset"));
e.addSuppressed(new IOException());
throw e;
}
Path output = invocationOnMock.getArgument(5, Path.class);
try (OutputStream outputStream = output.getOutputStream()) {
ByteStreams.copy(new ByteArrayInputStream(data), outputStream);
}

return null;
})
.when(downloader)
.download(any(), any(), any(), any(), any(), any(), any(), any(), any());

Path result =
download(
downloadManager,
ImmutableList.of(new URL("http://localhost")),
ImmutableMap.of(),
ImmutableMap.of(),
Optional.empty(),
"testCanonicalId",
Optional.empty(),
fs.getPath(workingDir.newFile().getAbsolutePath()),
eventHandler,
ImmutableMap.of(),
"testRepo");

assertThat(times.get()).isEqualTo(4);
String content = new String(ByteStreams.toByteArray(result.getInputStream()), UTF_8);
assertThat(content).isEqualTo("content");
}

public Path download(
DownloadManager downloadManager,
List<URL> originalUrls,
Expand Down
Loading