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

@@ -2,21 +2,30 @@ package io.github.hectorvent.floci.services.ecr.registry;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.services.ecr.EcrService;
import io.vertx.core.Future;
import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.HttpClient;
import io.vertx.core.http.HttpClientOptions;
import io.vertx.core.http.HttpClientRequest;
import io.vertx.core.http.HttpClientResponse;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.http.HttpServer;
import io.vertx.core.http.HttpServerRequest;
import io.vertx.core.http.HttpVersion;
import io.vertx.core.http.RequestOptions;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.vertx.core.net.SocketAddress;
import io.vertx.ext.web.Router;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
@@ -243,23 +252,7 @@ class EcrRegistryDataPlaneTest {
HttpServer dataPlane = null;
try {
EcrRegistryManager registryManager = Mockito.mock(EcrRegistryManager.class);
EcrService ecrService = Mockito.mock(EcrService.class);
EmulatorConfig config = Mockito.mock(EmulatorConfig.class, Mockito.RETURNS_DEEP_STUBS);
when(config.services().ecr().enabled()).thenReturn(true);
when(config.services().ecr().uriStyle()).thenReturn("hostname");
when(registryManager.httpClient())
.thenReturn(new RegistryHttpClient("http://127.0.0.1:" + registry.actualPort()));
when(ecrService.isImageTagImmutable("platform/api", "000000000000", "us-east-1"))
.thenReturn(true);
when(ecrService.registryRepositoryName("platform/api", "000000000000", "us-east-1"))
.thenReturn("legacy/platform-api");
Router router = Router.router(vertx);
new EcrRegistryDataPlane(registryManager, ecrService, config, vertx).register(router);
dataPlane = vertx.createHttpServer().requestHandler(router)
.listen(0, "127.0.0.1")
.toCompletionStage().toCompletableFuture().get(2, TimeUnit.SECONDS);
dataPlane = dataPlane(registry, true);
assertEquals(201, putManifest(dataPlane.actualPort()).statusCode());
@@ -307,23 +300,7 @@ class EcrRegistryDataPlaneTest {
HttpServer dataPlane = null;
try {
EcrRegistryManager registryManager = Mockito.mock(EcrRegistryManager.class);
EcrService ecrService = Mockito.mock(EcrService.class);
EmulatorConfig config = Mockito.mock(EmulatorConfig.class, Mockito.RETURNS_DEEP_STUBS);
when(config.services().ecr().enabled()).thenReturn(true);
when(config.services().ecr().uriStyle()).thenReturn("hostname");
when(registryManager.httpClient())
.thenReturn(new RegistryHttpClient("http://127.0.0.1:" + registry.actualPort()));
when(ecrService.isImageTagImmutable("platform/api", "000000000000", "us-east-1"))
.thenReturn(true);
when(ecrService.registryRepositoryName("platform/api", "000000000000", "us-east-1"))
.thenReturn("legacy/platform-api");
Router router = Router.router(vertx);
new EcrRegistryDataPlane(registryManager, ecrService, config, vertx).register(router);
dataPlane = vertx.createHttpServer().requestHandler(router)
.listen(0, "127.0.0.1")
.toCompletionStage().toCompletableFuture().get(2, TimeUnit.SECONDS);
dataPlane = dataPlane(registry, true);
String tlsHost = "000000000000.dkr.ecr.us-east-1.localhost.floci.io:4566";
assertEquals(201, putManifest(dataPlane.actualPort(), tlsHost).statusCode());
@@ -357,21 +334,7 @@ class EcrRegistryDataPlaneTest {
HttpServer dataPlane = null;
try {
EcrRegistryManager registryManager = Mockito.mock(EcrRegistryManager.class);
EcrService ecrService = Mockito.mock(EcrService.class);
EmulatorConfig config = Mockito.mock(EmulatorConfig.class, Mockito.RETURNS_DEEP_STUBS);
when(config.services().ecr().enabled()).thenReturn(true);
when(config.services().ecr().uriStyle()).thenReturn("hostname");
when(registryManager.httpClient())
.thenReturn(new RegistryHttpClient("http://127.0.0.1:" + registry.actualPort()));
when(ecrService.registryRepositoryName("platform/api", "000000000000", "us-east-1"))
.thenReturn("legacy/platform-api");
Router router = Router.router(vertx);
new EcrRegistryDataPlane(registryManager, ecrService, config, vertx).register(router);
dataPlane = vertx.createHttpServer().requestHandler(router)
.listen(0, "127.0.0.1")
.toCompletionStage().toCompletableFuture().get(2, TimeUnit.SECONDS);
dataPlane = dataPlane(registry, false);
List<String> sent = List.of(
"application/vnd.docker.distribution.manifest.v2+json",
@@ -401,6 +364,215 @@ class EcrRegistryDataPlaneTest {
}
}
/**
* An HTTP/2 client such as crane sends every upload of a push as a stream on one
* connection, and the data plane needs a registry connection for each. An upload left
* waiting for one stays paused, and its unread bytes hold the client connection's
* flow-control window: the uploads that do have a registry connection stall and never
* give it back.
*/
@Test
void concurrentHttp2UploadsOnOneConnectionAllReachTheRegistry() throws Exception {
int uploads = 8;
Buffer layer = Buffer.buffer(new byte[1024 * 1024]);
AtomicInteger bytesReceived = new AtomicInteger();
CountDownLatch registryConnectionsClosed = new CountDownLatch(uploads);
HttpServer registry = vertx.createHttpServer()
.connectionHandler(connection -> connection.closeHandler(closed -> registryConnectionsClosed.countDown()))
.requestHandler(request -> request.body().onSuccess(body -> {
bytesReceived.addAndGet(body.length());
request.response().setStatusCode(202).end();
}))
.listen(0, "127.0.0.1")
.toCompletionStage().toCompletableFuture().get(2, TimeUnit.SECONDS);
HttpServer dataPlane = null;
HttpClient client = null;
try {
dataPlane = dataPlane(registry, false);
// HTTP/2 with prior knowledge. The client pools a single HTTP/2 connection by
// default, so every upload is a stream on the same connection, as crane's are.
client = vertx.createHttpClient(new HttpClientOptions()
.setProtocolVersion(HttpVersion.HTTP_2)
.setHttp2ClearTextUpgrade(false));
SocketAddress server = SocketAddress.inetSocketAddress(dataPlane.actualPort(), "127.0.0.1");
List<Future<HttpClientResponse>> responses = new ArrayList<>();
for (int i = 0; i < uploads; i++) {
responses.add(client.request(new RequestOptions()
.setServer(server)
.setHost("000000000000.dkr.ecr.us-east-1.localhost")
.setPort(4566)
.setMethod(HttpMethod.PATCH)
.setURI("/v2/platform/api/blobs/uploads/upload-" + i))
.compose(request -> request.send(layer)));
}
Future.all(responses).toCompletionStage().toCompletableFuture().get(10, TimeUnit.SECONDS);
for (Future<HttpClientResponse> response : responses) {
assertEquals(HttpVersion.HTTP_2, response.result().version());
assertEquals(202, response.result().statusCode());
}
assertEquals(uploads * layer.length(), bytesReceived.get());
assertTrue(registryConnectionsClosed.await(5, TimeUnit.SECONDS),
"every exchange closes the registry connection it opened");
} finally {
if (client != null) {
client.close().toCompletionStage().toCompletableFuture().get(2, TimeUnit.SECONDS);
}
if (dataPlane != null) {
dataPlane.close().toCompletionStage().toCompletableFuture().get(2, TimeUnit.SECONDS);
}
registry.close().toCompletionStage().toCompletableFuture().get(2, TimeUnit.SECONDS);
}
}
/**
* An HTTP/2 client that resets an upload's stream mid-body ends the exchange: the data plane
* closes the registry connection it opened for it, which would otherwise wait for the rest of
* the declared Content-Length.
*/
@Test
void anUploadResetMidBodyClosesItsRegistryConnection() throws Exception {
CountDownLatch registryConnectionClosed = new CountDownLatch(1);
CountDownLatch uploadStarted = new CountDownLatch(1);
HttpServer registry = vertx.createHttpServer()
.connectionHandler(connection -> connection.closeHandler(closed -> registryConnectionClosed.countDown()))
.requestHandler(request -> request.handler(chunk -> uploadStarted.countDown()))
.listen(0, "127.0.0.1")
.toCompletionStage().toCompletableFuture().get(2, TimeUnit.SECONDS);
HttpServer dataPlane = null;
HttpClient client = null;
try {
dataPlane = dataPlane(registry, false);
client = vertx.createHttpClient(new HttpClientOptions()
.setProtocolVersion(HttpVersion.HTTP_2)
.setHttp2ClearTextUpgrade(false));
HttpClientRequest upload = client.request(new RequestOptions()
.setServer(SocketAddress.inetSocketAddress(dataPlane.actualPort(), "127.0.0.1"))
.setHost("000000000000.dkr.ecr.us-east-1.localhost")
.setPort(4566)
.setMethod(HttpMethod.PATCH)
.setURI("/v2/platform/api/blobs/uploads/abandoned"))
.toCompletionStage().toCompletableFuture().get(2, TimeUnit.SECONDS);
assertEquals(HttpVersion.HTTP_2, upload.version());
upload.putHeader("Content-Length", String.valueOf(1024 * 1024));
upload.write(Buffer.buffer(new byte[64 * 1024]));
assertTrue(uploadStarted.await(5, TimeUnit.SECONDS), "the upload reached the registry");
upload.reset();
assertTrue(registryConnectionClosed.await(5, TimeUnit.SECONDS),
"the abandoned exchange closes its registry connection");
} finally {
if (client != null) {
client.close().toCompletionStage().toCompletableFuture().get(2, TimeUnit.SECONDS);
}
if (dataPlane != null) {
dataPlane.close().toCompletionStage().toCompletableFuture().get(2, TimeUnit.SECONDS);
}
registry.close().toCompletionStage().toCompletableFuture().get(2, TimeUnit.SECONDS);
}
}
/**
* A client that resets its stream while the data plane is still checking an immutable tag ends
* the exchange there: nothing of it reaches the registry, and the tag is free for the next
* write.
*/
@Test
void aWriteResetDuringTheTagCheckNeverReachesTheRegistryAndFreesTheTag() throws Exception {
AtomicReference<HttpServerRequest> heldCheck = new AtomicReference<>();
CountDownLatch checkArrived = new CountDownLatch(1);
AtomicInteger manifestWrites = new AtomicInteger();
AtomicReference<String> manifestBody = new AtomicReference<>();
HttpServer registry = vertx.createHttpServer()
.requestHandler(request -> {
if (request.method() == HttpMethod.HEAD) {
// The first check waits until the test answers it; later ones find no tag.
if (heldCheck.compareAndSet(null, request)) {
checkArrived.countDown();
} else {
request.response().setStatusCode(404).end();
}
} else if (request.method() == HttpMethod.PUT) {
manifestWrites.incrementAndGet();
request.body().onSuccess(body -> {
manifestBody.set(body.toString());
request.response().setStatusCode(201).end();
});
} else {
request.response().setStatusCode(405).end();
}
})
.listen(0, "127.0.0.1")
.toCompletionStage().toCompletableFuture().get(2, TimeUnit.SECONDS);
HttpServer dataPlane = null;
HttpClient client = null;
try {
dataPlane = dataPlane(registry, true);
client = vertx.createHttpClient(new HttpClientOptions()
.setProtocolVersion(HttpVersion.HTTP_2)
.setHttp2ClearTextUpgrade(false));
HttpClientRequest write = client.request(new RequestOptions()
.setServer(SocketAddress.inetSocketAddress(dataPlane.actualPort(), "127.0.0.1"))
.setHost("000000000000.dkr.ecr.us-east-1.localhost")
.setPort(4566)
.setMethod(HttpMethod.PUT)
.setURI("/v2/platform/api/manifests/v1"))
.toCompletionStage().toCompletableFuture().get(2, TimeUnit.SECONDS);
assertEquals(HttpVersion.HTTP_2, write.version());
write.putHeader("Content-Length", "64");
write.write(Buffer.buffer("{\"abandoned\""));
assertTrue(checkArrived.await(5, TimeUnit.SECONDS), "the data plane checks the tag first");
write.reset();
// A PING answered after the reset means the data plane has read the reset first.
write.connection().ping(Buffer.buffer("12345678"))
.toCompletionStage().toCompletableFuture().get(2, TimeUnit.SECONDS);
heldCheck.get().response().setStatusCode(404).end();
assertEquals(201, putManifest(dataPlane.actualPort()).statusCode());
assertEquals(1, manifestWrites.get());
assertEquals("{}", manifestBody.get());
} finally {
if (client != null) {
client.close().toCompletionStage().toCompletableFuture().get(2, TimeUnit.SECONDS);
}
if (dataPlane != null) {
dataPlane.close().toCompletionStage().toCompletableFuture().get(2, TimeUnit.SECONDS);
}
registry.close().toCompletionStage().toCompletableFuture().get(2, TimeUnit.SECONDS);
}
}
/**
* A data plane in front of {@code registry}, on plain HTTP that a client can speak HTTP/1.1 or,
* with prior knowledge, HTTP/2 to. Repository {@code platform/api} of account 000000000000 in
* us-east-1 is stored as {@code legacy/platform-api}, with immutable tags when asked.
*/
private HttpServer dataPlane(HttpServer registry, boolean immutableTags) throws Exception {
EcrRegistryManager registryManager = Mockito.mock(EcrRegistryManager.class);
EcrService ecrService = Mockito.mock(EcrService.class);
EmulatorConfig config = Mockito.mock(EmulatorConfig.class, Mockito.RETURNS_DEEP_STUBS);
when(config.services().ecr().enabled()).thenReturn(true);
when(config.services().ecr().uriStyle()).thenReturn("hostname");
when(registryManager.httpClient())
.thenReturn(new RegistryHttpClient("http://127.0.0.1:" + registry.actualPort()));
when(ecrService.isImageTagImmutable("platform/api", "000000000000", "us-east-1"))
.thenReturn(immutableTags);
when(ecrService.registryRepositoryName("platform/api", "000000000000", "us-east-1"))
.thenReturn("legacy/platform-api");
Router router = Router.router(vertx);
new EcrRegistryDataPlane(registryManager, ecrService, config, vertx).register(router);
return vertx.createHttpServer().requestHandler(router)
.listen(0, "127.0.0.1")
.toCompletionStage().toCompletableFuture().get(2, TimeUnit.SECONDS);
}
private record ManifestResponse(int statusCode, String body) {
}