perf(codeartifact): parallelize per-version metadata fetches in ListPackageVersions (#6262)

Qodo follow-up from #6204: PypiserverSidecarClient and ReposiliteSidecarClient each made
one sequential HTTP request per version (a HEAD per pypi release, a details GET per Maven
version) before CodeArtifactService paginated, so even a one-result page waited on every
version's own round trip.

Both now run their per-version requests through a new shared SidecarConcurrency.mapBounded,
capped at 8 concurrent requests per sidecar container. The cap is enforced by a shared,
per-container permit (keyed by pypiserver's repositoryContainerId, or a single constant for
Reposilite's one shared container) rather than a per-call-only limit, so two simultaneous
listPackageVersions calls against the same container still can't exceed 8 requests between
them. Tasks run on a virtual-thread-per-task executor, not a platform-thread pool: mapBounded
submits every item up front and lets the semaphore do the real gating, so a package with
thousands of versions doesn't start that many platform threads per call, each just parked in
Semaphore.acquire(). A failure (or the caller being interrupted) also cancels the rest of the
call's work, instead of leaving it running against the sidecar after the call has already
failed. Results stay in input order and a failing item still fails the whole call, matching
the original sequential behavior exactly.

Verified with unit tests for the helper (ordering, the per-container concurrency cap, the
cap holding across two simultaneous calls sharing a container, exception propagation,
cancellation of the rest of the work after an early failure) and a 12-version Docker-gated
test per format against the real pypiserver and Reposilite sidecars, on top of the existing
single-version coverage.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-10-22 22:21:52 -05:00
1 parent 6bc598fbfd
commit 581cd62790
6 files changed
+422 -11

No files matched your search

@@ -149,6 +149,12 @@ public class PypiserverSidecarClient implements RepositorySidecarManager {
* lands on disk within the same upload, close enough that picking "the first one" over "the
* most recently modified one" makes no practical difference, and the JSON listing gives no
* way to know which upload order they actually arrived in regardless.
*
* <p>The per-version {@code HEAD} requests run through {@link SidecarConcurrency#mapBounded},
* not a plain sequential loop: a package with many versions used to make every one of those
* requests one at a time before returning even a single-result page. Keyed by {@code
* repositoryContainerId} so the concurrency cap holds across simultaneous calls against the
* same pypiserver container, not just within one of them.
*/
@Override
public List<PackageVersionTimestamp> listPackageVersions(String repositoryContainerId, String domain,
@@ -178,13 +184,13 @@ public class PypiserverSidecarClient implements RepositorySidecarManager {
} catch (Exception e) {
throw new IllegalStateException("Could not parse pypiserver's version listing for " + packageName, e);
}
List<PackageVersionTimestamp> result = new ArrayList<>();
releases.fieldNames().forEachRemaining(version -> {
List<String> versions = new ArrayList<>();
releases.fieldNames().forEachRemaining(versions::add);
return SidecarConcurrency.mapBounded(repositoryContainerId, versions, version -> {
JsonNode files = releases.path(version);
String fileUrl = files.isEmpty() ? "" : files.get(0).path("url").asText("");
result.add(new PackageVersionTimestamp(version, fileUrl.isEmpty() ? null : lastModified(fileUrl)));
return new PackageVersionTimestamp(version, fileUrl.isEmpty() ? null : lastModified(fileUrl));
});
return result;
}
private Instant lastModified(String fileUrl) {
@@ -42,6 +42,13 @@ public class ReposiliteSidecarClient implements RepositorySidecarManager {
private static final String FORMAT = "maven";
private static final Logger LOG = Logger.getLogger(ReposiliteSidecarClient.class);
private static final String MAVEN_SETTINGS_PATH = "/api/settings/domain/maven";
/**
* {@link SidecarConcurrency#mapBounded}'s key for every {@code listPackageVersions} call: one
* shared constant, since {@link ReposiliteSidecarManager} backs every CodeArtifact Maven
* repository with the same single container, unlike pypiserver's one-container-per-repository
* shape.
*/
private static final String SHARED_CONTAINER_KEY = "reposilite";
private static final int REPOSITORY_READY_POLL_MAX_MS = 3_000;
private static final int REPOSITORY_READY_POLL_INTERVAL_MS = 50;
/** Real Maven metadata's own documented {@code lastUpdated} shape: UTC, {@code yyyyMMddHHmmss}. */
@@ -274,6 +281,12 @@ public class ReposiliteSidecarClient implements RepositorySidecarManager {
* "when this version became complete." A version directory with no {@code FILE} entries at
* all (never happens through a real deploy, but not assumed impossible) reports no timestamp
* rather than guessing one.
*
* <p>The per-version details requests run through {@link SidecarConcurrency#mapBounded}, not a
* plain sequential loop: an artifact with many versions used to make every one of those
* requests one at a time before returning even a single-result page. Keyed by {@link
* #SHARED_CONTAINER_KEY} so the concurrency cap holds across simultaneous calls too, since
* every Maven repository shares the one Reposilite container.
*/
@Override
public List<PackageVersionTimestamp> listPackageVersions(String repositoryContainerId, String domain,
@@ -295,16 +308,15 @@ public class ReposiliteSidecarClient implements RepositorySidecarManager {
throw new IllegalStateException("Could not list versions of " + packageName + " on the Reposilite "
+ "sidecar: upstream returned " + artifactResponse.statusCode());
}
List<PackageVersionTimestamp> result = new ArrayList<>();
List<String> versions = new ArrayList<>();
for (JsonNode entry : readTree(artifactResponse.body()).path("files")) {
if (!"DIRECTORY".equals(entry.path("type").asText())) {
continue;
if ("DIRECTORY".equals(entry.path("type").asText())) {
versions.add(entry.path("name").asText());
}
String version = entry.path("name").asText();
result.add(new PackageVersionTimestamp(version, latestFileTimestamp(baseUrl,
versionDetailsPath(repositoryContainerId, namespace, packageName, version))));
}
return result;
return SidecarConcurrency.mapBounded(SHARED_CONTAINER_KEY, versions, version -> new PackageVersionTimestamp(
version, latestFileTimestamp(baseUrl, versionDetailsPath(repositoryContainerId, namespace,
packageName, version))));
}
private Instant latestFileTimestamp(String baseUrl, String versionDetailsPath) {
@@ -0,0 +1,131 @@
package io.github.hectorvent.floci.services.codeartifact;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.Semaphore;
import java.util.function.Function;
/**
* Bounds the fan-out {@code listPackageVersions} needs when a sidecar has no single call that
* returns every version's publish time at once: pypiserver answers with no timestamp field at
* all (one {@code HEAD} per version), and Reposilite's per-version details page is the only way
* to read a version's own file timestamps (one {@code GET} per version). Running those
* sequentially means a one-result page still waits on every version in the package; running them
* all at once risks opening as many sockets against the sidecar container as the package has
* versions. Package-private: both callers live in this package, and the ordering/exception
* guarantees below are shaped around what {@code listPackageVersions} specifically needs, not a
* general-purpose utility.
*/
final class SidecarConcurrency {
/**
* How many per-item requests run against one sidecar container at once, no matter how many
* {@code listPackageVersions} calls against that same container are in flight at the same
* time: large enough that a package with a realistic number of versions sees a real speedup,
* small enough that the container backing a sidecar can't be flooded with hundreds of
* simultaneous connections.
*/
private static final int MAX_CONCURRENT_REQUESTS = 8;
/**
* One {@link Semaphore} per sidecar container, shared across every {@code mapBounded} call for
* that container so {@link #MAX_CONCURRENT_REQUESTS} holds in aggregate: a per-call-only limit
* would let two simultaneous {@code listPackageVersions} calls against the same container each
* open that many connections, doubling the real cap. The key is whatever identity a caller
* already has for "which container": pypiserver's own {@code repositoryContainerId} (one
* container per CodeArtifact repository), or a single shared constant for Reposilite (one
* container for every Maven repository).
*/
private static final Map<Object, Semaphore> SIDECAR_PERMITS = new ConcurrentHashMap<>();
/**
* Virtual threads, not a platform-thread pool: {@code mapBounded} submits every item up front and
* lets the semaphore above do the actual gating, so a package with thousands of versions would
* otherwise start as many platform threads per call, each parked in {@link Semaphore#acquire()}.
* Same approach {@link io.github.hectorvent.floci.services.codepipeline.CodePipelineService} already
* uses for its own I/O-bound fan-out.
*/
private static final ExecutorService EXECUTOR = Executors.newVirtualThreadPerTaskExecutor();
private SidecarConcurrency() {
}
/**
* Applies {@code perItem} to every element of {@code items}, in parallel up to {@link
* #MAX_CONCURRENT_REQUESTS} at a time against {@code sidecarKey}'s container, and returns the
* results in the same order as {@code items}. A single item's failure fails the whole call,
* exactly as the original sequential loops this replaces did: the first failure's own
* exception propagates (unwrapped from the {@link ExecutionException} every {@link Future}
* wraps it in), the same error the caller would have seen from a plain sequential loop that
* stopped on that item.
*
* <p>Every other item is then cancelled too: one still queued never starts at all, and one
* already running is interrupted. Without this, a failure (or the caller thread being
* interrupted) would leave the rest of the package's requests running against the sidecar in
* the background even after the call itself has already failed.
*
* <p>Zero or one item skips the executor and the permit entirely and calls {@code perItem}
* directly on the calling thread, since there is nothing to parallelize and nothing worth
* bounding for it.
*/
static <T, R> List<R> mapBounded(Object sidecarKey, List<T> items, Function<T, R> perItem) {
if (items.size() <= 1) {
List<R> result = new ArrayList<>(items.size());
for (T item : items) {
result.add(perItem.apply(item));
}
return result;
}
Semaphore permits = SIDECAR_PERMITS.computeIfAbsent(sidecarKey, key -> new Semaphore(MAX_CONCURRENT_REQUESTS));
List<Future<R>> futures = new ArrayList<>(items.size());
for (T item : items) {
futures.add(EXECUTOR.submit(() -> runWithPermit(permits, perItem, item)));
}
boolean succeeded = false;
try {
List<R> results = new ArrayList<>(items.size());
for (Future<R> future : futures) {
results.add(await(future));
}
succeeded = true;
return results;
} finally {
if (!succeeded) {
for (Future<R> future : futures) {
future.cancel(true);
}
}
}
}
private static <T, R> R runWithPermit(Semaphore permits, Function<T, R> perItem, T item)
throws InterruptedException {
permits.acquire();
try {
return perItem.apply(item);
} finally {
permits.release();
}
}
private static <R> R await(Future<R> future) {
try {
return future.get();
} catch (ExecutionException e) {
Throwable cause = e.getCause();
if (cause instanceof RuntimeException runtimeException) {
throw runtimeException;
}
throw new IllegalStateException(cause);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new IllegalStateException("Interrupted while waiting on a sidecar request", e);
}
}
}
@@ -20,7 +20,10 @@ import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@@ -590,6 +593,45 @@ class CodeArtifactMavenDockerIntegrationTest {
.body("versions[0].origin.originType", equalTo("INTERNAL"));
}
/**
* The real-world gap this guards against: {@code ReposiliteSidecarClient#listPackageVersions}
* used to fetch every version's publish-time details page one at a time, so a package with
* many versions still answered no faster than its slowest version's own round trip, times the
* version count. This deploys enough versions that a broken fan-out (dropped, duplicated, or
* silently empty results from the bounded executor) would show up as something other than
* exactly this set of versions, against the real Reposilite sidecar, not a mock.
*/
@Test
@Order(22)
void listPackageVersionsFetchesManyVersionsPublishTimesCorrectly() {
String packageName = "manyversions";
int versionCount = 12;
Set<String> expectedVersions = new HashSet<>();
for (int i = 1; i <= versionCount; i++) {
String version = "1.0." + i;
expectedVersions.add(version);
byte[] content = ("many-versions-bytes-" + i).getBytes(StandardCharsets.UTF_8);
given().header("Authorization", "Bearer " + bearerToken).body(content)
.put("/codeartifact/maven/" + DOMAIN + "/" + REPO + "/com/example/" + packageName + "/" + version
+ "/" + packageName + "-" + version + ".jar")
.then().statusCode(200);
}
JsonPath body = given().header("Authorization", AUTH)
.post("/v1/package/versions?domain=" + DOMAIN + "&repository=" + REPO + "&format=maven"
+ "&namespace=com.example&package=" + packageName)
.then().statusCode(200)
.body("package", equalTo(packageName))
.body("versions.size()", equalTo(versionCount))
.extract().jsonPath();
List<String> actualVersions = body.getList("versions.version", String.class);
assertEquals(expectedVersions, new HashSet<>(actualVersions));
String defaultDisplayVersion = body.getString("defaultDisplayVersion");
assertTrue(expectedVersions.contains(defaultDisplayVersion),
"expected defaultDisplayVersion to be one of the deployed versions, got "
+ defaultDisplayVersion);
}
/**
* A throwaway groupId/artifactId, not {@link #GAV}: deleting it exercises Reposilite's real
* directory delete (confirmed live before this was built: a single {@code DELETE} on the
@@ -18,8 +18,10 @@ import org.junit.jupiter.api.TestMethodOrder;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@@ -728,6 +730,47 @@ class CodeArtifactPypiDockerIntegrationTest {
throw new AssertionError("expected an asset named " + name + " among " + assets);
}
/**
* The real-world gap this guards against: {@code PypiserverSidecarClient#listPackageVersions}
* used to send one {@code HEAD} per version, one at a time, so a package with many versions
* answered no faster than its slowest version's own round trip, times the version count. This
* uploads enough versions that a broken fan-out (dropped, duplicated, or silently empty results
* from the bounded executor) would show up as something other than exactly this set of
* versions, against the real pypiserver sidecar, not a mock.
*/
@Test
@Order(21)
void listPackageVersionsFetchesManyVersionsPublishTimesCorrectly() {
String packageName = "many-versions-pypi-pkg";
int versionCount = 12;
Set<String> expectedVersions = new HashSet<>();
for (int i = 1; i <= versionCount; i++) {
String version = "1.0." + i;
expectedVersions.add(version);
String filename = "many_versions_pypi_pkg-" + version + ".tar.gz";
byte[] content = ("many-versions-sdist-bytes-" + i).getBytes(StandardCharsets.UTF_8);
given().header("Authorization", "Bearer " + bearerToken)
.contentType("multipart/form-data; boundary=boundary123")
.body(twineUploadBody("boundary123", packageName, filename, content))
.post("/codeartifact/pypi/" + DOMAIN + "/" + REPO + "/")
.then().statusCode(200);
}
JsonPath body = given().header("Authorization", AUTH)
.post("/v1/package/versions?domain=" + DOMAIN + "&repository=" + REPO + "&format=pypi"
+ "&package=" + packageName)
.then().statusCode(200)
.body("package", equalTo(packageName))
.body("versions.size()", equalTo(versionCount))
.extract().jsonPath();
List<String> actualVersions = body.getList("versions.version", String.class);
assertEquals(expectedVersions, new HashSet<>(actualVersions));
String defaultDisplayVersion = body.getString("defaultDisplayVersion");
assertTrue(expectedVersions.contains(defaultDisplayVersion),
"expected defaultDisplayVersion to be one of the uploaded versions, got "
+ defaultDisplayVersion);
}
/**
* Proves the actual mechanism, not just that {@code PypiserverSidecarManager} has a method
* named right: {@code ContainerTeardowns.stopAll} runs every {@code ContainerTeardown} on
@@ -0,0 +1,177 @@
package io.github.hectorvent.floci.services.codeartifact;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.lessThanOrEqualTo;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
/**
* Focused unit coverage for the bounded-fan-out helper {@code PypiserverSidecarClient} and {@code
* ReposiliteSidecarClient} both use to parallelize their per-version {@code listPackageVersions}
* requests. These tests use plain in-process work, not real HTTP, to pin down the properties that
* matter: results come back in input order regardless of completion order, more than {@code
* MAX_CONCURRENT_REQUESTS} items never run at once against one sidecar key (even across two
* simultaneous calls sharing that key), a failing item fails the whole call with its own real
* exception rather than a wrapped or swallowed one, and the rest of the work is cancelled rather
* than left running after a failure.
*/
class SidecarConcurrencyTest {
@Test
void mapBoundedPreservesInputOrderRegardlessOfCompletionOrder() {
List<Integer> items = List.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
List<String> result = SidecarConcurrency.mapBounded(new Object(), items, i -> {
// Later items sleep less, so they would finish first if order depended on completion.
try {
Thread.sleep((11 - i) % 4);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
fail("interrupted");
}
return "v" + i;
});
assertThat(result, equalTo(List.of("v1", "v2", "v3", "v4", "v5", "v6", "v7", "v8", "v9", "v10")));
}
@Test
void mapBoundedNeverRunsMoreThanTheConcurrencyCapAtOnce() throws InterruptedException {
int itemCount = 32;
List<Integer> items = new ArrayList<>(itemCount);
for (int i = 0; i < itemCount; i++) {
items.add(i);
}
AtomicInteger concurrent = new AtomicInteger();
AtomicInteger maxObserved = new AtomicInteger();
CountDownLatch allDone = new CountDownLatch(1);
Thread worker = new Thread(() -> {
SidecarConcurrency.mapBounded(new Object(), items, i -> {
int current = concurrent.incrementAndGet();
maxObserved.getAndUpdate(previous -> Math.max(previous, current));
try {
Thread.sleep(20);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
concurrent.decrementAndGet();
return i;
});
allDone.countDown();
});
worker.start();
assertTrue(allDone.await(10, TimeUnit.SECONDS), "mapBounded did not finish in time");
assertTrue(maxObserved.get() <= 8, "expected at most 8 concurrent items, saw " + maxObserved.get());
assertTrue(maxObserved.get() > 1, "expected real parallelism, saw " + maxObserved.get());
}
@Test
void mapBoundedCapsConcurrencyAcrossSimultaneousCallsSharingTheSameSidecarKey() throws InterruptedException {
Object sharedKey = new Object();
int itemsPerCall = 6;
List<Integer> items = new ArrayList<>(itemsPerCall);
for (int i = 0; i < itemsPerCall; i++) {
items.add(i);
}
AtomicInteger concurrent = new AtomicInteger();
AtomicInteger maxObserved = new AtomicInteger();
Runnable call = () -> SidecarConcurrency.mapBounded(sharedKey, items, i -> {
int current = concurrent.incrementAndGet();
maxObserved.getAndUpdate(previous -> Math.max(previous, current));
try {
Thread.sleep(50);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
concurrent.decrementAndGet();
return i;
});
Thread callA = new Thread(call);
Thread callB = new Thread(call);
callA.start();
callB.start();
callA.join(10_000);
callB.join(10_000);
assertThat("two simultaneous calls sharing one sidecar key must not exceed the shared cap in aggregate",
maxObserved.get(), lessThanOrEqualTo(8));
assertThat("expected the two calls' work to actually overlap under the shared cap", maxObserved.get(),
greaterThan(itemsPerCall));
}
@Test
void mapBoundedPropagatesARuntimeExceptionFromAFailingItemUnwrapped() {
List<Integer> items = List.of(1, 2, 3);
IllegalStateException thrown = assertThrows(IllegalStateException.class, () -> SidecarConcurrency.mapBounded(
new Object(), items, i -> {
if (i == 2) {
throw new IllegalStateException("boom on " + i);
}
return i;
}));
assertThat(thrown.getMessage(), equalTo("boom on 2"));
}
@Test
void mapBoundedCancelsTheOtherItemAfterAnEarlyFailure() throws InterruptedException {
CountDownLatch otherItemStarted = new CountDownLatch(1);
CountDownLatch otherItemFinished = new CountDownLatch(1);
CountDownLatch neverOpens = new CountDownLatch(1);
AtomicBoolean otherItemInterrupted = new AtomicBoolean(false);
assertThrows(IllegalStateException.class, () -> SidecarConcurrency.mapBounded(new Object(), List.of(1, 2),
i -> {
if (i == 1) {
// Only fail once item 2 is actually running and blocked, so this exercises real
// cancellation of in-flight work instead of racing against whether item 2's task
// had even been scheduled yet.
boolean started;
try {
started = otherItemStarted.await(5, TimeUnit.SECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
started = false;
}
if (!started) {
throw new IllegalStateException("item 2 never started");
}
throw new IllegalStateException("boom");
}
otherItemStarted.countDown();
try {
neverOpens.await();
} catch (InterruptedException e) {
otherItemInterrupted.set(true);
Thread.currentThread().interrupt();
} finally {
otherItemFinished.countDown();
}
return i;
}));
assertTrue(otherItemFinished.await(5, TimeUnit.SECONDS), "expected mapBounded to cancel item 2 once item "
+ "1 had already failed, instead of leaving it blocked indefinitely");
assertTrue(otherItemInterrupted.get(), "expected item 2 to be interrupted rather than complete normally "
+ "after item 1 had already failed");
}
@Test
void mapBoundedCallsPerItemDirectlyWhenThereIsAtMostOneItem() {
Set<String> threadNames = ConcurrentHashMap.newKeySet();
List<Integer> result = SidecarConcurrency.mapBounded(new Object(), List.of(42), i -> {
threadNames.add(Thread.currentThread().getName());
return i;
});
assertThat(result, equalTo(List.of(42)));
assertThat(threadNames, equalTo(Set.of(Thread.currentThread().getName())));
}
}