fix(ecr): give every data plane exchange its own registry connection (#5883)

* fix(ecr): give every data plane exchange its own registry connection

An HTTP/2 client such as crane sends a whole push as streams on one
connection. The data plane paused each stream until the shared Vert.x
client handed it a registry connection, from a pool of five. With more
uploads than that, the waiting streams kept their unread bytes, which
hold the client connection's flow-control window; the five uploads that
had a registry connection stopped receiving data, never finished, and
never gave their connection back. Every later registry request,
GET /v2/ included, then waited behind them until Floci restarted.

A larger pool only moves the threshold: HTTP/2 allows any number of
concurrent streams, and several client connections can fill a pool of
any size while each still has a stream waiting. So each exchange now
opens a client of its own and closes it when the exchange ends, and no
request ever waits for a connection another request holds.

An exchange also ends when its client goes away mid-upload (#5573). A
client that reset its HTTP/2 stream or closed its connection left the
registry request waiting for the rest of a body with a Content-Length,
which registry:2 never answers, and the exchange kept its connection and
its immutable tag lock. The response's close handler, which HTTP/2
always calls when the stream closes, and the request's exception handler
now end the exchange, closing its client and so its registry connection.
The end runs once: the close handler also fires after an exchange that
ended normally, by which time another exchange may hold the same
immutable tag, which a second release would drop.

Tests: eight HTTP/2 uploads on one connection all reach the registry and
every exchange closes the connection it opened (they time out on main);
an upload reset mid-body closes its registry connection (it stays open
on main). The data plane tests build their data plane through one helper
instead of four copies of the same mocks, router and server.

* fix(ecr): end an exchange whose client leaves during the tag check

The handlers that end an exchange when its client goes away were set only
once the registry check of an immutable tag had finished. A client that
reset its stream during that check was never seen: the exchange then
proxied a request that was already gone, its registry request waited for
a body that never came, and the tag stayed locked, so every later write
of it was refused as if the tag existed.

The exchange now starts before the check, with those handlers, and the
check's result is dropped when the client has already left: nothing of it
reaches the registry, and its tag is free at once. Once an exchange has a
registry client, its tag is freed only when that client has closed the
registry connection, so another write of the tag cannot start its check
while this one can still reach the registry. A failure answer is no longer
written to a client that went away.

Tests: a write reset during the tag check never reaches the registry and
the next write of the tag succeeds (on the previous head it is refused
with TAG_INVALID); the two HTTP/2 tests now assert that their exchanges
are HTTP/2.
This commit is contained in:
Alessandro Lattao authored and GitHub committed 2026-10-17 15:46:53 -04:00
1 parent 553411bc70
commit de94e3cfcc
2 files changed
+303 -71

No files matched your search

@@ -24,6 +24,7 @@ import java.nio.charset.StandardCharsets;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -45,7 +46,6 @@ public class EcrRegistryDataPlane {
private final EcrService ecrService;
private final EmulatorConfig config;
private final Vertx vertx;
private final HttpClient proxyClient;
private final Set<String> inFlightImmutableTags = ConcurrentHashMap.newKeySet();
@Inject
@@ -57,9 +57,6 @@ public class EcrRegistryDataPlane {
this.ecrService = ecrService;
this.config = config;
this.vertx = vertx;
this.proxyClient = vertx.createHttpClient(new HttpClientOptions()
.setConnectTimeout(5_000)
.setKeepAlive(true));
}
void register(@Observes Router router) {
@@ -91,7 +88,14 @@ public class EcrRegistryDataPlane {
return;
}
checkTagAndProxy(context, registryRequest, immutableTag);
Exchange exchange = new Exchange(immutableTag);
// A client that goes away, by resetting its HTTP/2 stream or closing the connection, ends
// the exchange at any point, the registry check included: otherwise the registry keeps
// waiting for the rest of a body with a Content-Length, and the exchange's connection,
// client and immutable tag lock stay.
context.response().closeHandler(closed -> exchange.end());
context.request().exceptionHandler(failure -> exchange.end());
checkTagAndProxy(context, registryRequest, exchange);
}
private String immutableTag(RoutingContext context, RegistryRequest request) {
@@ -102,7 +106,8 @@ public class EcrRegistryDataPlane {
return request.accountId() + "/" + request.region() + "/" + request.repositoryName() + ":" + request.tag();
}
private void checkTagAndProxy(RoutingContext context, RegistryRequest request, String immutableTag) {
private void checkTagAndProxy(RoutingContext context, RegistryRequest request, Exchange exchange) {
String immutableTag = exchange.immutableTag;
vertx.<Boolean>executeBlocking(promise -> {
try {
registryManager.ensureStarted();
@@ -118,57 +123,72 @@ public class EcrRegistryDataPlane {
promise.fail(e);
}
}).onComplete(result -> {
if (exchange.ended()) {
// The client went away during the check: nothing of this exchange reaches the
// registry.
return;
}
if (result.failed()) {
context.request().resume();
release(immutableTag);
exchange.end();
context.response().setStatusCode(503).end();
} else if (result.result()) {
context.request().resume();
release(immutableTag);
exchange.end();
imageTagAlreadyExists(context, request);
} else {
proxy(context, request, immutableTag);
proxy(context, request, exchange);
}
});
}
private void proxy(RoutingContext context, RegistryRequest request, String immutableTag) {
private void proxy(RoutingContext context, RegistryRequest request, Exchange exchange) {
URI backend = URI.create(registryManager.httpClient().baseUrl());
RequestOptions options = new RequestOptions()
.setHost(backend.getHost())
.setPort(backend.getPort() == -1 ? 80 : backend.getPort())
.setURI(request.backendUri())
.setMethod(context.request().method());
proxyClient.request(options).onComplete(upstreamRequest -> {
// A registry connection of its own for every exchange, never a place in a shared pool's
// queue. An HTTP/2 client such as crane sends a whole push as streams on one connection,
// so a pool of any size can be full of its uploads while another of its streams waits,
// paused. The waiting stream's unread bytes hold that connection's flow-control window,
// the uploads that do have a registry connection stop receiving data, and none of them
// ever gives its connection back.
HttpClient client = exchange.open(vertx.createHttpClient(new HttpClientOptions().setConnectTimeout(5_000)));
client.request(options).onComplete(upstreamRequest -> {
if (upstreamRequest.failed()) {
context.request().resume();
release(immutableTag);
context.response().setStatusCode(503).end();
exchange.end();
failUnlessGone(context, 503);
return;
}
HttpClientRequest clientReq = upstreamRequest.result();
copyRequestHeaders(context, clientReq);
clientReq.response().onComplete(upstreamResponse -> {
if (upstreamResponse.failed()) {
release(immutableTag);
if (!context.response().ended()) {
context.response().setStatusCode(502).end();
}
exchange.end();
failUnlessGone(context, 502);
return;
}
copyResponseHeaders(context, upstreamResponse.result(), request);
upstreamResponse.result().pipeTo(context.response()).onComplete(ignored -> release(immutableTag));
upstreamResponse.result().pipeTo(context.response()).onComplete(ignored -> exchange.end());
});
clientReq.send(context.request()).onFailure(ignored -> {
release(immutableTag);
if (!context.response().ended()) {
context.response().setStatusCode(502).end();
}
exchange.end();
failUnlessGone(context, 502);
});
context.request().resume();
});
}
/** Answers {@code status} unless the response is already written or its client went away. */
private static void failUnlessGone(RoutingContext context, int status) {
if (!context.response().ended() && !context.response().closed()) {
context.response().setStatusCode(status).end();
}
}
private static void copyRequestHeaders(RoutingContext context, HttpClientRequest upstream) {
context.request().headers().forEach(header -> {
if (!HOP_BY_HOP_HEADERS.contains(header.getKey().toLowerCase())
@@ -226,6 +246,46 @@ public class EcrRegistryDataPlane {
}
}
/**
* One proxied request: the registry client it opens once it gets that far, and the immutable
* tag it holds. It ends once, at whichever comes first of the registry's answer, a failure and
* the client going away; the close handler also fires after an exchange that ended normally,
* when another exchange may already hold the same tag.
*/
private final class Exchange {
private final String immutableTag;
private final AtomicBoolean ended = new AtomicBoolean();
private HttpClient client;
private Exchange(String immutableTag) {
this.immutableTag = immutableTag;
}
private boolean ended() {
return ended.get();
}
private HttpClient open(HttpClient registryClient) {
client = registryClient;
return registryClient;
}
private void end() {
if (!ended.compareAndSet(false, true)) {
return;
}
if (client == null) {
release(immutableTag);
return;
}
// Closing the client closes the exchange's registry connection, which the registry
// sees and aborts on. The tag is free only once it is closed, so another write of the
// tag cannot start its check while this one can still reach the registry.
client.close().onComplete(closed -> release(immutableTag));
}
}
static Optional<RegistryRequest> requestFor(String host, String path, String query, String uriStyle) {
String registryPath = "/v2".equals(path) ? "/v2/" : path;
if (!registryPath.startsWith("/v2/")) {