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

Enhance error reporting for TychoRepositoryTransport #4626

Merged
merged 1 commit into from
Jan 22, 2025
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 @@ -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 @@ -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() {
Expand Down Expand Up @@ -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
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);
}

}
Loading