Skip to content

Commit

Permalink
Enhance error reporting for TychoRepositoryTransport
Browse files Browse the repository at this point in the history
  • Loading branch information
laeubi committed Jan 22, 2025
1 parent 4101a12 commit f8cfc4e
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
@Component(role = TransportCacheConfig.class)
public class DefaultTransportCacheConfig implements TransportCacheConfig, Initializable {

private static final boolean DEBUG_REQUESTS = Boolean.getBoolean("tycho.p2.transport.debug");

private boolean offline;
private boolean update;
private boolean interactive;
Expand Down Expand Up @@ -86,4 +88,9 @@ public File getCacheLocation() {
return cacheLocation;
}

@Override
public boolean isDebug() {
return DEBUG_REQUESTS;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,9 @@ public synchronized File fetchFile(URI uri, HttpTransportFactory transportFactor
updateHeader(response, code);
if (isRedirected(code)) {
URI redirect = getRedirect(uri);
if (cacheConfig.isDebug()) {
logger.info("Redirect (code = " + code + ") from " + uri + " to " + redirect);
}
if (code == HttpURLConnection.HTTP_MOVED_TEMP) {
// https://github.com/eclipse-tycho/tycho/issues/4459
// Don't save temporary redirects since they might change later, rendering the
Expand Down Expand Up @@ -311,7 +314,7 @@ public synchronized File getFile(URI uri, HttpTransportFactory transportFactory,
throw new FileNotFoundException(uri.toString());
}
if (isRedirected(code)) {
return SharedHttpCacheStorage.this.getCacheEntry(getRedirect(uri), logger)
return SharedHttpCacheStorage.this.getCacheEntry(getRedirect(uri, logger), logger)
.getCacheFile(transportFactory);
}
if (file.isFile()) {
Expand Down Expand Up @@ -430,12 +433,17 @@ public int getResponseCode() {
return Integer.parseInt(getHeader().getProperty(RESPONSE_CODE, "-1"));
}

public URI getRedirect(URI base) throws FileNotFoundException {
public URI getRedirect(URI base, Logger logger) throws FileNotFoundException {
String location = getHeader().getProperty("location");
if (location == null) {
throw new FileNotFoundException(base.toASCIIString());
}
return base.resolve(location);
URI uri = base.resolve(location);
if (cacheConfig.isDebug()) {
logger.info(
"location given by header = '" + location + "', with base =" + base + ", resolves = " + uri);
}
return uri;
}

public Properties getHeader() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,7 @@ public interface TransportCacheConfig {

boolean isInteractive();

boolean isDebug();

File getCacheLocation();
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ public class TychoRepositoryTransport extends org.eclipse.equinox.internal.p2.re

private static final int MAX_DOWNLOAD_THREADS = Integer.getInteger("tycho.p2.transport.max-download-threads", 4);

private static final boolean DEBUG_REQUESTS = Boolean.getBoolean("tycho.p2.transport.debug");

private static final Executor DOWNLOAD_EXECUTOR = Executors.newFixedThreadPool(MAX_DOWNLOAD_THREADS,
new ThreadFactory() {

Expand Down Expand Up @@ -125,13 +123,12 @@ public IStatus downloadArtifact(URI source, OutputStream target, IArtifactDescri
}
return reportStatus(downloadStatus, target);
} catch (AuthenticationFailedException e) {
return reportStatus(Status.error("authentication failed for " + source, e), target);
return reportStatus(statusWithCode("authentication failed for %s",
ProvisionException.REPOSITORY_FAILED_AUTHENTICATION, source, e), target);
} catch (IOException e) {
if (e instanceof HttpTimeoutException) {
return reportStatus(new Status(IStatus.ERROR,
TychoRepositoryTransport.class,
ProvisionException.REPOSITORY_FAILED_READ,
"download from " + source + " timed out", e), target);
return reportStatus(statusWithCode("download from %s timed out",
ProvisionException.REPOSITORY_FAILED_READ, source, e), target);
}
return reportStatus(Status.error("download from " + source + " failed", e), target);
} catch (CoreException e) {
Expand All @@ -142,8 +139,9 @@ public IStatus downloadArtifact(URI source, OutputStream target, IArtifactDescri
@Override
public InputStream stream(URI toDownload, IProgressMonitor monitor)
throws FileNotFoundException, CoreException, AuthenticationFailedException {
if (DEBUG_REQUESTS) {
logger.debug("Request stream for " + toDownload);
boolean debug = cacheConfig.isDebug();
if (debug) {
logger.info("Request stream for " + toDownload);
}
requests.increment();
if (toDownload.toASCIIString().endsWith("p2.index")) {
Expand All @@ -154,28 +152,35 @@ public InputStream stream(URI toDownload, IProgressMonitor monitor)
if (handler != null) {
File cachedFile = handler.getFile(toDownload);
if (cachedFile != null) {
if (DEBUG_REQUESTS) {
if (debug) {
logger.debug(" --> routed through handler " + handler.getClass().getSimpleName());
}
return new FileInputStream(cachedFile);
}
}
return toDownload.toURL().openStream();
} catch (FileNotFoundException e) {
if (DEBUG_REQUESTS) {
logger.debug(" --> not found!");
if (debug) {
logger.info(" --> not found! (" + toDownload + ")");
}
throw e;
} catch (java.net.http.HttpTimeoutException timeout) {
if (debug) {
logger.info(" --> timeout while requesting (" + toDownload + "): " + timeout);
}
throw new CoreException(statusWithCode("download from %s timed out",
ProvisionException.REPOSITORY_FAILED_READ, toDownload, timeout));
} catch (IOException e) {
if (e instanceof AuthenticationFailedException afe) {
throw afe;
}
if (DEBUG_REQUESTS) {
logger.debug(" --> generic error: " + e);
if (debug) {
logger.info(" --> generic error (" + toDownload + "): " + e);
}
throw new CoreException(Status.error("download from " + toDownload + " failed", e));
throw new CoreException(statusWithCode("download from %s failed", ProvisionException.REPOSITORY_FAILED_READ,
toDownload, e));
} finally {
if (DEBUG_REQUESTS) {
if (debug) {
logger.debug("Total number of requests: " + requests.longValue() + " (" + indexRequests.longValue()
+ " for p2.index)");
}
Expand Down Expand Up @@ -264,4 +269,9 @@ private static IStatus reportStatus(IStatus status, OutputStream target) {
return status;
}

private static Status statusWithCode(String message, int code, URI source, Exception causingException) {
return new Status(IStatus.ERROR, TychoRepositoryTransport.class, code, String.format(message, source),
causingException);
}

}

0 comments on commit f8cfc4e

Please sign in to comment.