From 1c490cb86d183ab0abadfab44a41cfb951f892ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20L=C3=A4ubrich?= Date: Wed, 22 Jan 2025 09:07:38 +0100 Subject: [PATCH] Enhance error reporting for TychoRepositoryTransport --- .../DefaultTransportCacheConfig.java | 7 ++++ .../transport/SharedHttpCacheStorage.java | 18 +++++--- .../transport/TransportCacheConfig.java | 2 + .../transport/TychoRepositoryTransport.java | 42 ++++++++++++------- 4 files changed, 48 insertions(+), 21 deletions(-) diff --git a/p2-maven-plugin/src/main/java/org/eclipse/tycho/p2maven/transport/DefaultTransportCacheConfig.java b/p2-maven-plugin/src/main/java/org/eclipse/tycho/p2maven/transport/DefaultTransportCacheConfig.java index ee894b8eae..37f0e61530 100644 --- a/p2-maven-plugin/src/main/java/org/eclipse/tycho/p2maven/transport/DefaultTransportCacheConfig.java +++ b/p2-maven-plugin/src/main/java/org/eclipse/tycho/p2maven/transport/DefaultTransportCacheConfig.java @@ -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; @@ -86,4 +88,9 @@ public File getCacheLocation() { return cacheLocation; } + @Override + public boolean isDebug() { + return DEBUG_REQUESTS; + } + } diff --git a/p2-maven-plugin/src/main/java/org/eclipse/tycho/p2maven/transport/SharedHttpCacheStorage.java b/p2-maven-plugin/src/main/java/org/eclipse/tycho/p2maven/transport/SharedHttpCacheStorage.java index 07fa2532cc..96acaf5966 100644 --- a/p2-maven-plugin/src/main/java/org/eclipse/tycho/p2maven/transport/SharedHttpCacheStorage.java +++ b/p2-maven-plugin/src/main/java/org/eclipse/tycho/p2maven/transport/SharedHttpCacheStorage.java @@ -90,7 +90,7 @@ public CacheEntry getCacheEntry(URI uri, Logger logger) throws FileNotFoundExcep throw new FileNotFoundException(normalized.toASCIIString()); } if (code == HttpURLConnection.HTTP_MOVED_PERM) { - return getCacheEntry(cacheLine.getRedirect(normalized), logger); + return getCacheEntry(cacheLine.getRedirect(normalized, logger), logger); } } return new CacheEntry() { @@ -252,7 +252,10 @@ public synchronized File fetchFile(URI uri, HttpTransportFactory transportFactor } updateHeader(response, code); if (isRedirected(code)) { - URI redirect = getRedirect(uri); + URI redirect = getRedirect(uri, logger); + 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 @@ -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()) { @@ -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() { diff --git a/p2-maven-plugin/src/main/java/org/eclipse/tycho/p2maven/transport/TransportCacheConfig.java b/p2-maven-plugin/src/main/java/org/eclipse/tycho/p2maven/transport/TransportCacheConfig.java index fa59627ad2..c00009253f 100644 --- a/p2-maven-plugin/src/main/java/org/eclipse/tycho/p2maven/transport/TransportCacheConfig.java +++ b/p2-maven-plugin/src/main/java/org/eclipse/tycho/p2maven/transport/TransportCacheConfig.java @@ -22,5 +22,7 @@ public interface TransportCacheConfig { boolean isInteractive(); + boolean isDebug(); + File getCacheLocation(); } diff --git a/p2-maven-plugin/src/main/java/org/eclipse/tycho/p2maven/transport/TychoRepositoryTransport.java b/p2-maven-plugin/src/main/java/org/eclipse/tycho/p2maven/transport/TychoRepositoryTransport.java index ac3596d85e..1a87dd2cf5 100644 --- a/p2-maven-plugin/src/main/java/org/eclipse/tycho/p2maven/transport/TychoRepositoryTransport.java +++ b/p2-maven-plugin/src/main/java/org/eclipse/tycho/p2maven/transport/TychoRepositoryTransport.java @@ -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() { @@ -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) { @@ -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")) { @@ -154,7 +152,7 @@ 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); @@ -162,20 +160,27 @@ public InputStream stream(URI toDownload, IProgressMonitor monitor) } 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)"); } @@ -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); + } + }