Skip to content

Commit

Permalink
Report on download cache look up and archive extraction
Browse files Browse the repository at this point in the history
Checking an entry in the download cache or a distdir might take a
while, as the hash of that file has to be computed. Extracting an
archive can take even more time, as that usually implies a lot of
disk IO. Inform the user about the progress here to avoid confusing
this delay with network activity.

Change-Id: I86316529309960a8a501b96ae752a12a554197b9
PiperOrigin-RevId: 284140963
  • Loading branch information
aehlig authored and copybara-github committed Dec 6, 2019
1 parent 3fd70e9 commit e810227
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,17 @@ public Path download(
String cacheKey = checksum.get().toString();
KeyType cacheKeyType = checksum.get().getKeyType();
try {
eventHandler.post(
new CacheProgress(mainUrl.toString(), "Checking in " + cacheKeyType + " cache"));
String currentChecksum = RepositoryCache.getChecksum(cacheKeyType, destination);
if (currentChecksum.equals(cacheKey)) {
// No need to download.
return destination;
}
} catch (IOException e) {
// Ignore error trying to hash. We'll attempt to retrieve from cache or just download again.
} finally {
eventHandler.post(new CacheProgress(mainUrl.toString()));
}

if (repositoryCache.isEnabled()) {
Expand Down Expand Up @@ -171,11 +175,16 @@ public Path download(
boolean match = false;
Path candidate = dir.getRelative(name);
try {
eventHandler.post(
new CacheProgress(
mainUrl.toString(), "Checking " + cacheKeyType + " of " + candidate));
match = RepositoryCache.getChecksum(cacheKeyType, candidate).equals(cacheKey);
} catch (IOException e) {
// Not finding anything in a distdir is a normal case, so handle it absolutely
// quietly. In fact, it is common to specify a whole list of dist dirs,
// with the assumption that only one will contain an entry.
} finally {
eventHandler.post(new CacheProgress(mainUrl.toString()));
}
if (match) {
if (isCachingByProvidedChecksum) {
Expand Down Expand Up @@ -304,4 +313,37 @@ private static ImmutableSet<String> getCandidateFileNames(URL url, Path destinat
return ImmutableSet.of(destination.getBaseName());
}
}

private static class CacheProgress implements ExtendedEventHandler.FetchProgress {
private final String originalUrl;
private final String progress;
private final boolean isFinished;

CacheProgress(String originalUrl, String progress) {
this.originalUrl = originalUrl;
this.progress = progress;
this.isFinished = false;
}

CacheProgress(String originalUrl) {
this.originalUrl = originalUrl;
this.progress = "";
this.isFinished = true;
}

@Override
public String getResourceIdentifier() {
return originalUrl;
}

@Override
public String getProgress() {
return progress;
}

@Override
public boolean isFinished() {
return isFinished;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,10 @@ public void extract(Object archive, Object output, String stripPrefix, Location
location);
env.getListener().post(w);

env.getListener()
.post(
new ExtractProgress(
outputPath.getPath().toString(), "Extracting " + archivePath.getPath()));
DecompressorValue.decompress(
DecompressorDescriptor.builder()
.setTargetKind(rule.getTargetKind())
Expand All @@ -721,6 +725,7 @@ public void extract(Object archive, Object output, String stripPrefix, Location
.setRepositoryPath(outputPath.getPath())
.setPrefix(stripPrefix)
.build());
env.getListener().post(new ExtractProgress(outputPath.getPath().toString()));
}

@Override
Expand Down Expand Up @@ -803,6 +808,9 @@ public StructImpl downloadAndExtract(
env.getListener().post(w);
try (SilentCloseable c =
Profiler.instance().profile("extracting: " + rule.getLabel().toString())) {
env.getListener()
.post(
new ExtractProgress(outputPath.getPath().toString(), "Extracting " + downloadedPath));
DecompressorValue.decompress(
DecompressorDescriptor.builder()
.setTargetKind(rule.getTargetKind())
Expand All @@ -811,6 +819,7 @@ public StructImpl downloadAndExtract(
.setRepositoryPath(outputPath.getPath())
.setPrefix(stripPrefix)
.build());
env.getListener().post(new ExtractProgress(outputPath.getPath().toString()));
}

StructImpl downloadResult = calculateDownloadResult(checksum, downloadedPath);
Expand Down Expand Up @@ -1085,4 +1094,37 @@ private static Map<URI, Map<String, String>> getAuthHeaders(Map<String, Dict<?,
}
return headers.build();
}

private static class ExtractProgress implements FetchProgress {
private final String repositoryPath;
private final String progress;
private final boolean isFinished;

ExtractProgress(String repositoryPath, String progress) {
this.repositoryPath = repositoryPath;
this.progress = progress;
this.isFinished = false;
}

ExtractProgress(String repositoryPath) {
this.repositoryPath = repositoryPath;
this.progress = "";
this.isFinished = true;
}

@Override
public String getResourceIdentifier() {
return repositoryPath;
}

@Override
public String getProgress() {
return progress;
}

@Override
public boolean isFinished() {
return isFinished;
}
}
}

0 comments on commit e810227

Please sign in to comment.