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

Add ResolutionEventListener#downloadMetadataSuccess to communicate long download times to consumers of MavenPomDownloader #4566

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import org.jspecify.annotations.Nullable;
import org.openrewrite.scheduling.RecipeRunCycle;

import java.net.URI;
import java.time.Duration;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
Expand Down Expand Up @@ -100,6 +102,10 @@ default void putCurrentRecipe(Recipe recipe) {

BiConsumer<Throwable, ExecutionContext> getOnTimeout();

default @Nullable BiConsumer<URI, Duration> getOnDownload() {
return null;
}

default int getCycle() {
return getCycleDetails().getCycle();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import org.jspecify.annotations.Nullable;

import java.net.URI;
import java.time.Duration;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
Expand All @@ -27,6 +28,8 @@ public class InMemoryExecutionContext implements ExecutionContext {
private final Map<String, Object> messages = new ConcurrentHashMap<>();
private final Consumer<Throwable> onError;
private final BiConsumer<Throwable, ExecutionContext> onTimeout;
@Nullable
private final BiConsumer<URI, Duration> onDownload;

public InMemoryExecutionContext() {
this(
Expand All @@ -44,9 +47,19 @@ public InMemoryExecutionContext(Consumer<Throwable> onError, Duration runTimeout
});
}

public InMemoryExecutionContext(Consumer<Throwable> onError, Duration runTimeout, BiConsumer<Throwable, ExecutionContext> onTimeout) {
public InMemoryExecutionContext(Consumer<Throwable> onError,
Duration runTimeout,
BiConsumer<Throwable, ExecutionContext> onTimeout) {
this(onError, runTimeout, onTimeout, null);
}

public InMemoryExecutionContext(Consumer<Throwable> onError,
Duration runTimeout,
BiConsumer<Throwable, ExecutionContext> onTimeout,
@Nullable BiConsumer<URI, Duration> onDownload) {
this.onError = onError;
this.onTimeout = onTimeout;
this.onDownload = onDownload;
putMessage(ExecutionContext.RUN_TIMEOUT, runTimeout);
}

Expand Down Expand Up @@ -80,4 +93,10 @@ public Consumer<Throwable> getOnError() {
public BiConsumer<Throwable, ExecutionContext> getOnTimeout() {
return onTimeout;
}

@Override
@Nullable
public BiConsumer<URI, Duration> getOnDownload() {
bryceatmoderne marked this conversation as resolved.
Show resolved Hide resolved
return onDownload;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import org.jspecify.annotations.Nullable;
import org.openrewrite.ExecutionContext;

import java.net.URI;
import java.time.Duration;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

Expand Down Expand Up @@ -64,4 +66,10 @@ public Consumer<Throwable> getOnError() {
public BiConsumer<Throwable, ExecutionContext> getOnTimeout() {
return delegate.getOnTimeout();
}

@Override
@Nullable
public BiConsumer<URI, Duration> getOnDownload() {
bryceatmoderne marked this conversation as resolved.
Show resolved Hide resolved
return delegate.getOnDownload();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@
import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.toList;

@SuppressWarnings("OptionalAssignedToNull")
public class MavenPomDownloader {
private static final RetryPolicy<Object> retryPolicy = RetryPolicy.builder()
.handle(SocketTimeoutException.class, TimeoutException.class)
Expand Down Expand Up @@ -333,7 +332,12 @@ public MavenMetadata downloadMetadata(GroupArtifactVersion gav, @Nullable Resolv
.setRepositoryResponses(repositoryResponses);
}

sample.stop(timer.tags("outcome", "success").register(Metrics.globalRegistry));
long nanos = sample.stop(timer.tags("outcome", "success").register(Metrics.globalRegistry));
if (ctx.getOnDownload() != null) {
ctx.getOnDownload().accept(
URI.create(attemptedUris.get(attemptedUris.size() - 1)),
Duration.ofNanos(nanos));
}
return mavenMetadata;
}

Expand Down Expand Up @@ -604,6 +608,10 @@ public Pom download(GroupArtifactVersion gav,
mavenCache.putPom(resolvedGav, pom);
ctx.getResolutionListener().downloadSuccess(resolvedGav, containingPom);
sample.stop(timer.tags("outcome", "downloaded").register(Metrics.globalRegistry));
long nanos = sample.stop(timer.tags("outcome", "downloaded").register(Metrics.globalRegistry));
if (ctx.getOnDownload() != null) {
ctx.getOnDownload().accept(uri, Duration.ofNanos(nanos));
}
return pom;
} catch (HttpSenderResponseException e) {
repositoryResponses.put(repo, e.getMessage());
Expand Down
Loading