diff --git a/docs/services/bedrock-runtime.md b/docs/services/bedrock-runtime.md index 0cf34c3506..4e6a7ff6b0 100644 --- a/docs/services/bedrock-runtime.md +++ b/docs/services/bedrock-runtime.md @@ -52,6 +52,16 @@ export FLOCI_SERVICES_BEDROCK_RUNTIME_PROXY_DEFAULT_MODEL=llama3.2 Useful for integration testing: applications that call Bedrock directly don't need to stand up their own stub/mock of Bedrock — point the existing SDK client at Floci and get real responses from whatever OpenAI-compatible backend is already available, with the stubbing concern handled by Floci instead of the application under test. +### Upstream failures and caller cancellation + +When a caller disconnects during `Converse` or `ConverseStream`, the proxy cancels +pending upstream requests and closes streaming response bodies, including streams +that stall between tokens. The model server controls how it reacts to that signal. + +Operational HTTP-error warnings retain the upstream status code and omit the +backend response body. A connected caller receives `ModelErrorException` with a +bounded backend error description. Streaming error-body reads are limited to 8 KiB. + ## Examples ### Structured outputs through the proxy diff --git a/src/main/java/io/github/hectorvent/floci/services/bedrockruntime/backend/ProxyBackend.java b/src/main/java/io/github/hectorvent/floci/services/bedrockruntime/backend/ProxyBackend.java index e593f4bcd7..131e683290 100644 --- a/src/main/java/io/github/hectorvent/floci/services/bedrockruntime/backend/ProxyBackend.java +++ b/src/main/java/io/github/hectorvent/floci/services/bedrockruntime/backend/ProxyBackend.java @@ -5,21 +5,33 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ObjectNode; import io.github.hectorvent.floci.config.EmulatorConfig; import io.github.hectorvent.floci.core.common.AwsException; +import io.quarkus.vertx.http.runtime.CurrentVertxRequest; +import io.vertx.ext.web.RoutingContext; import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; import org.jboss.logging.Logger; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; import java.io.OutputStream; +import java.io.UncheckedIOException; import java.net.URI; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; import java.net.http.HttpTimeoutException; +import java.nio.charset.StandardCharsets; import java.time.Duration; import java.util.LinkedHashMap; import java.util.Map; +import java.util.concurrent.CancellationException; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.function.BooleanSupplier; import java.util.function.Consumer; -import java.util.stream.Collectors; import java.util.stream.Stream; /** @@ -34,6 +46,7 @@ public class ProxyBackend implements BedrockBackend { private final ObjectMapper objectMapper; private final EmulatorConfig config; + private final CurrentVertxRequest currentRequest; // Config is immutable for the process's lifetime, so the mapping string is parsed // once here rather than on every Converse request. @@ -45,9 +58,10 @@ public class ProxyBackend implements BedrockBackend { .build(); @Inject - public ProxyBackend(ObjectMapper objectMapper, EmulatorConfig config) { + public ProxyBackend(ObjectMapper objectMapper, EmulatorConfig config, CurrentVertxRequest currentRequest) { this.objectMapper = objectMapper; this.config = config; + this.currentRequest = currentRequest; this.modelMapping = parseModelMapping(config.services().bedrockRuntime().proxy().modelMapping().orElse("")); } @@ -96,39 +110,122 @@ public class ProxyBackend implements BedrockBackend { // BedrockOpenAiTranslator.streamBedrockEvents). HttpRequest request = buildHttpRequest(proxyConfig, openAiRequest); long startNanos = System.nanoTime(); - HttpResponse> response = invoke(modelId, request, HttpResponse.BodyHandlers.ofLines()); - - if (response.statusCode() >= 300) { - String errorBody; - try (Stream body = response.body()) { - errorBody = body.limit(50).collect(Collectors.joining("\n")); + // A stalled upstream must be closed on caller disconnect without waiting for another token. + RoutingContext context = currentRequest.getCurrent(); + CompletableFuture> pending = httpClient.sendAsync(request, HttpResponse.BodyHandlers.ofInputStream()); + AtomicBoolean complete = new AtomicBoolean(); + Thread monitor = monitorCancellation(context.response()::closed, complete, () -> { + pending.thenAccept(response -> closeBody(response.body())); + pending.cancel(true); + }); + HttpResponse response; + try { + response = pending.get(); + if (response.statusCode() >= 300) { + String errorBody; + try { + errorBody = new String(response.body().readNBytes(8192), StandardCharsets.UTF_8); + } catch (IOException unavailable) { + LOG.warnv("Failed to read Bedrock proxy error response: modelId={0}, status={1}", + modelId, response.statusCode()); + errorBody = ""; + } finally { + closeBody(response.body()); + } + throw failedRequest(response.statusCode(), errorBody); } - throw failedRequest(response.statusCode(), errorBody); + } catch (InterruptedException interrupted) { + Thread.currentThread().interrupt(); + pending.thenAccept(completed -> closeBody(completed.body())); + pending.cancel(true); + complete.set(true); + monitor.interrupt(); + throw new AwsException("ModelErrorException", "Proxy stream interrupted.", 424); + } catch (ExecutionException | CancellationException failure) { + complete.set(true); + monitor.interrupt(); + if (failure.getCause() instanceof HttpTimeoutException) { + LOG.warnv("Bedrock proxy backend timed out: modelId={0}", modelId); + throw new AwsException("ModelTimeoutException", "Proxy stream timed out.", 408); + } + if (failure instanceof ExecutionException) { + LOG.warnv("Bedrock proxy backend call failed: modelId={0}", modelId); + } + throw new AwsException("ModelErrorException", "Proxy stream unavailable or disconnected.", 424); + } catch (RuntimeException failure) { + complete.set(true); + monitor.interrupt(); + throw failure; } + return output -> { + try (InputStream body = response.body(); + Stream lines = new BufferedReader(new InputStreamReader(body, StandardCharsets.UTF_8)).lines()) { + BedrockOpenAiTranslator.streamBedrockEvents(objectMapper, lines, output, startNanos); + } catch (IOException failure) { + throw new UncheckedIOException(failure); + } finally { + complete.set(true); + monitor.interrupt(); + } + }; + } - return output -> BedrockOpenAiTranslator.streamBedrockEvents(objectMapper, response.body(), output, startNanos); + private static void closeBody(InputStream body) { + try { + body.close(); + } catch (IOException ignored) { + // The transport is already disconnected; no response data belongs in diagnostics. + } } private static AwsException failedRequest(int response, String errorBody) { - LOG.warnv("Bedrock proxy backend returned HTTP {0}: {1}", response, errorBody); + LOG.warnv("Bedrock proxy backend returned HTTP {0}", response); return new AwsException("ModelErrorException", "Proxy backend returned HTTP %d: %s".formatted(response, truncate(errorBody, 512)), 424); } private HttpResponse invoke(String modelId, HttpRequest request, HttpResponse.BodyHandler bodyHandler) { + RoutingContext context = currentRequest.getCurrent(); + CompletableFuture> pending = httpClient.sendAsync(request, bodyHandler); + AtomicBoolean complete = new AtomicBoolean(); + Thread monitor = monitorCancellation(context.response()::closed, complete, () -> pending.cancel(true)); try { - return httpClient.send(request, bodyHandler); - } catch (HttpTimeoutException e) { - LOG.warnv("Bedrock proxy backend timed out: modelId={0}, url={1}, error={2}", - modelId, request.uri(), e.getMessage()); - throw new AwsException("ModelTimeoutException", "Proxy backend timed out: " + e.getMessage(), 408); - } catch (Exception e) { - LOG.warnv("Bedrock proxy backend call failed: modelId={0}, url={1}, error={2}", - modelId, request.uri(), e.getMessage()); - throw new AwsException("ModelErrorException", "Failed to reach proxy backend: " + e.getMessage(), 424); + return pending.get(); + } catch (InterruptedException interrupted) { + Thread.currentThread().interrupt(); + pending.cancel(true); + throw new AwsException("ModelErrorException", "Proxy request interrupted.", 424); + } catch (ExecutionException | CancellationException failure) { + if (failure.getCause() instanceof HttpTimeoutException) { + LOG.warnv("Bedrock proxy backend timed out: modelId={0}", modelId); + throw new AwsException("ModelTimeoutException", "Proxy backend timed out.", 408); + } + if (failure instanceof ExecutionException) { + LOG.warnv("Bedrock proxy backend call failed: modelId={0}", modelId); + } + throw new AwsException("ModelErrorException", "Proxy backend unavailable or disconnected.", 424); + } finally { + complete.set(true); + monitor.interrupt(); } } + private static Thread monitorCancellation(BooleanSupplier shouldCancel, AtomicBoolean complete, Runnable cancelAction) { + return Thread.ofVirtual().name("bedrock-proxy-cancellation").start(() -> { + try { + while (!complete.get()) { + if (shouldCancel.getAsBoolean()) { + cancelAction.run(); + return; + } + Thread.sleep(100); + } + } catch (InterruptedException interrupted) { + Thread.currentThread().interrupt(); + } + }); + } + private HttpRequest buildHttpRequest(EmulatorConfig.BedrockProxyConfig proxyConfig, ObjectNode openAiRequest) { String baseUrl = proxyConfig.url() .filter(url -> !url.isBlank()) diff --git a/src/test/java/io/github/hectorvent/floci/services/bedrockruntime/BedrockProxyCancellationIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/bedrockruntime/BedrockProxyCancellationIntegrationTest.java new file mode 100644 index 0000000000..67f5670014 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/bedrockruntime/BedrockProxyCancellationIntegrationTest.java @@ -0,0 +1,181 @@ +package io.github.hectorvent.floci.services.bedrockruntime; + +import io.quarkus.test.common.http.TestHTTPResource; +import io.quarkus.test.junit.QuarkusTest; +import io.quarkus.test.junit.QuarkusTestProfile; +import io.quarkus.test.junit.TestProfile; +import org.junit.jupiter.api.Test; + +import java.io.InputStream; +import java.io.OutputStream; +import java.net.ServerSocket; +import java.net.Socket; +import java.net.SocketException; +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.nio.charset.StandardCharsets; +import java.time.Duration; +import java.util.Map; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +@QuarkusTest +@TestProfile(BedrockProxyCancellationIntegrationTest.Profile.class) +class BedrockProxyCancellationIntegrationTest { + private static final int PORT = 18939; + @TestHTTPResource + URI base; + + @Test + void disconnectClosesUpstreamWithoutAnotherToken() throws Exception { + CountDownLatch closed = new CountDownLatch(1); + try (ServerSocket server = new ServerSocket(PORT)) { + server.setSoTimeout(10000); + Thread upstream = Thread.ofVirtual().start(() -> { + try (Socket socket = server.accept()) { + socket.setSoTimeout(10000); + InputStream input = socket.getInputStream(); + StringBuilder header = new StringBuilder(); + while (!header.toString().endsWith("\r\n\r\n")) { + int next = input.read(); + if (next < 0 || header.length() > 65536) { + return; + } + header.append((char) next); + } + int bytes = 0; + for (String line : header.toString().split("\r\n")) { + if (line.toLowerCase().startsWith("content-length:")) { + bytes = Integer.parseInt(line.substring(line.indexOf(':') + 1).trim()); + } + } + assertEquals(bytes, input.readNBytes(bytes).length); + OutputStream output = socket.getOutputStream(); + output.write("HTTP/1.1 200 OK\r\nContent-Type: text/event-stream\r\nTransfer-Encoding: chunked\r\n\r\n".getBytes(StandardCharsets.US_ASCII)); + byte[] chunk = "data: {\"choices\":[{\"delta\":{\"content\":\"hello\"}}]}\n\n".getBytes(StandardCharsets.UTF_8); + output.write((Integer.toHexString(chunk.length) + "\r\n").getBytes(StandardCharsets.US_ASCII)); + output.write(chunk); + output.write("\r\n".getBytes(StandardCharsets.US_ASCII)); + output.flush(); + // No more output: disconnect must close the transport independently of token delivery. + try { + if (input.read() == -1) { + closed.countDown(); + } + } catch (SocketException disconnected) { + closed.countDown(); + } + } catch (Exception expected) { + // The latch remains unset so a backend setup failure cannot pass the assertion. + } + }); + try (HttpClient client = HttpClient.newHttpClient()) { + HttpRequest request = HttpRequest.newBuilder(base.resolve("/model/fixture/converse-stream")) + .timeout(Duration.ofSeconds(10)).header("Content-Type", "application/json") + .header("Authorization", "AWS4-HMAC-SHA256 Credential=AKID/20260101/us-east-1/bedrock/aws4_request") + .POST(HttpRequest.BodyPublishers.ofString("{\"messages\":[{\"role\":\"user\",\"content\":[{\"text\":\"synthetic\"}]}]}")) + .build(); + HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofInputStream()); + assertEquals(200, response.statusCode()); + try (InputStream body = response.body()) { + assertTrue(body.read() >= 0); + } + assertTrue(closed.await(5, TimeUnit.SECONDS), "Client disconnect must close the stalled upstream"); + } finally { + upstream.interrupt(); + upstream.join(1000); + } + } + } + + @Test + void nonStreamingDisconnectBeforeHeadersClosesUpstream() throws Exception { + disconnectWhileUpstreamStalls("converse", false); + } + + @Test + void streamingDisconnectBeforeHeadersClosesUpstream() throws Exception { + disconnectWhileUpstreamStalls("converse-stream", false); + } + + @Test + void nonStreamingDisconnectDuringBodyClosesUpstream() throws Exception { + disconnectWhileUpstreamStalls("converse", true); + } + + private void disconnectWhileUpstreamStalls(String route, boolean partialResponse) throws Exception { + CountDownLatch accepted = new CountDownLatch(1); + CountDownLatch closed = new CountDownLatch(1); + try (ServerSocket server = new ServerSocket(PORT)) { + server.setSoTimeout(10000); + Thread upstream = Thread.ofVirtual().start(() -> { + try (Socket socket = server.accept()) { + socket.setSoTimeout(10000); + InputStream input = socket.getInputStream(); + StringBuilder header = new StringBuilder(); + while (!header.toString().endsWith("\r\n\r\n")) { + int next = input.read(); + if (next < 0 || header.length() > 65536) { + return; + } + header.append((char) next); + } + int bytes = 0; + for (String line : header.toString().split("\r\n")) { + if (line.toLowerCase().startsWith("content-length:")) { + bytes = Integer.parseInt(line.substring(line.indexOf(':') + 1).trim()); + } + } + if (input.readNBytes(bytes).length != bytes) { + return; + } + if (partialResponse) { + socket.getOutputStream().write(("HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n" + + "Content-Length: 1000\r\n\r\n{").getBytes(StandardCharsets.US_ASCII)); + socket.getOutputStream().flush(); + } + accepted.countDown(); + // The upstream stalls before completing its response, so cancellation must close it. + try { + if (input.read() == -1) { + closed.countDown(); + } + } catch (SocketException disconnected) { + closed.countDown(); + } + } catch (Exception expected) { + // An unset latch exposes setup and transport failures to the test thread. + } + }); + try (Socket frontend = new Socket(base.getHost(), base.getPort())) { + String body = "{\"messages\":[{\"role\":\"user\",\"content\":[{\"text\":\"synthetic\"}]}]}"; + String request = "POST /model/fixture/" + route + " HTTP/1.1\r\nHost: localhost\r\n" + + "Content-Type: application/json\r\n" + + "Authorization: AWS4-HMAC-SHA256 Credential=AKID/20260101/us-east-1/bedrock/aws4_request\r\n" + + "Content-Length: " + body.getBytes(StandardCharsets.UTF_8).length + "\r\n\r\n" + body; + frontend.getOutputStream().write(request.getBytes(StandardCharsets.UTF_8)); + frontend.getOutputStream().flush(); + assertTrue(accepted.await(5, TimeUnit.SECONDS), "Upstream must receive the request before disconnect"); + frontend.close(); + assertTrue(closed.await(5, TimeUnit.SECONDS), "Disconnect must close the stalled upstream request"); + } finally { + upstream.interrupt(); + upstream.join(1000); + } + } + } + + public static final class Profile implements QuarkusTestProfile { + @Override + public Map getConfigOverrides() { + return Map.of("floci.services.bedrock-runtime.backend", "proxy", + "floci.services.bedrock-runtime.proxy.url", "http://127.0.0.1:" + PORT + "/v1", + "floci.services.bedrock-runtime.proxy.passthrough", "true"); + } + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/bedrockruntime/BedrockProxyLoggingIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/bedrockruntime/BedrockProxyLoggingIntegrationTest.java new file mode 100644 index 0000000000..7ec83fe5b5 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/bedrockruntime/BedrockProxyLoggingIntegrationTest.java @@ -0,0 +1,116 @@ +package io.github.hectorvent.floci.services.bedrockruntime; + +import com.sun.net.httpserver.HttpServer; +import io.github.hectorvent.floci.services.bedrockruntime.backend.ProxyBackend; +import io.github.hectorvent.floci.testutil.LogCapture; +import io.quarkus.test.common.http.TestHTTPResource; +import io.quarkus.test.junit.QuarkusTest; +import io.quarkus.test.junit.TestProfile; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import java.io.IOException; +import java.net.InetSocketAddress; +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.nio.charset.StandardCharsets; +import java.time.Duration; +import java.util.List; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import java.util.logging.Level; +import java.util.logging.LogRecord; +import java.util.logging.SimpleFormatter; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.equalTo; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +@QuarkusTest +@TestProfile(BedrockProxyCancellationIntegrationTest.Profile.class) +class BedrockProxyLoggingIntegrationTest { + + @TestHTTPResource + URI base; + + @ParameterizedTest + @ValueSource(strings = {"converse", "converse-stream"}) + void upstreamErrorIsLoggedWithoutResponseBody(String operation) throws IOException { + String marker = "synthetic-private-upstream-content"; + HttpServer backend = HttpServer.create(new InetSocketAddress("127.0.0.1", 18939), 0); + backend.createContext("/v1/chat/completions", exchange -> { + exchange.getRequestBody().readAllBytes(); + byte[] body = ("{\"error\":\"" + marker + "\"}").getBytes(StandardCharsets.UTF_8); + exchange.getResponseHeaders().add("Content-Type", "application/json"); + exchange.sendResponseHeaders(503, body.length); + try (exchange) { + exchange.getResponseBody().write(body); + } + }); + backend.start(); + try { + List records = LogCapture.capture(ProxyBackend.class, () -> + given() + .contentType("application/json") + .header("Authorization", "AWS4-HMAC-SHA256 Credential=AKID/20260101/us-east-1/bedrock/aws4_request") + .body("{\"messages\":[{\"role\":\"user\",\"content\":[{\"text\":\"synthetic\"}]}]}") + .when() + .post("/model/fixture/" + operation) + .then() + .statusCode(424) + .body("__type", equalTo("ModelErrorException")) + .body("message", containsString("HTTP 503")) + .body("message", containsString(marker))); + SimpleFormatter formatter = new SimpleFormatter(); + assertTrue(records.stream().anyMatch(record -> record.getLevel().equals(Level.WARNING) + && formatter.format(record).contains("HTTP 503")), "The upstream status must remain diagnosable"); + assertFalse(records.stream().anyMatch(record -> formatter.format(record).contains(marker)), + "Operational logs must not include upstream response bodies"); + } finally { + backend.stop(0); + } + } + + @Test + void streamingErrorBodyReadIsBoundedWithoutWaitingForEof() throws Exception { + CountDownLatch release = new CountDownLatch(1); + HttpServer backend = HttpServer.create(new InetSocketAddress("127.0.0.1", 18939), 0); + backend.createContext("/v1/chat/completions", exchange -> { + exchange.getRequestBody().readAllBytes(); + exchange.sendResponseHeaders(503, 32768); + try (exchange) { + exchange.getResponseBody().write("x".repeat(8192).getBytes(StandardCharsets.UTF_8)); + exchange.getResponseBody().flush(); + try { + release.await(10, TimeUnit.SECONDS); + } catch (InterruptedException interrupted) { + Thread.currentThread().interrupt(); + } + } + }); + backend.start(); + try (HttpClient client = HttpClient.newHttpClient()) { + HttpRequest request = HttpRequest.newBuilder(base.resolve("/model/fixture/converse-stream")) + .timeout(Duration.ofSeconds(5)) + .header("Content-Type", "application/json") + .header("Authorization", "AWS4-HMAC-SHA256 Credential=AKID/20260101/us-east-1/bedrock/aws4_request") + .POST(HttpRequest.BodyPublishers.ofString( + "{\"messages\":[{\"role\":\"user\",\"content\":[{\"text\":\"synthetic\"}]}]}")) + .build(); + HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); + assertEquals(424, response.statusCode()); + assertTrue(response.body().contains("ModelErrorException")); + assertTrue(response.body().contains("HTTP 503")); + assertTrue(response.body().length() < 1024, "Caller error descriptions must remain bounded"); + } finally { + release.countDown(); + backend.stop(0); + } + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/bedrockruntime/BedrockProxyTimeoutIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/bedrockruntime/BedrockProxyTimeoutIntegrationTest.java index bf31b5e009..2d624ee79a 100644 --- a/src/test/java/io/github/hectorvent/floci/services/bedrockruntime/BedrockProxyTimeoutIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/bedrockruntime/BedrockProxyTimeoutIntegrationTest.java @@ -6,7 +6,8 @@ import io.quarkus.test.junit.QuarkusTestProfile; import io.quarkus.test.junit.TestProfile; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; import java.io.IOException; import java.net.InetSocketAddress; @@ -57,8 +58,9 @@ class BedrockProxyTimeoutIntegrationTest { backend.stop(0); } - @Test - void converse_backendTimesOut_returnsModelTimeoutException() { + @ParameterizedTest + @ValueSource(strings = {"converse", "converse-stream"}) + void backendTimesOut_returnsModelTimeoutException(String operation) { given() .contentType("application/json") .header("Authorization", AUTH_HEADER) @@ -66,7 +68,7 @@ class BedrockProxyTimeoutIntegrationTest { {"messages": [{"role": "user", "content": [{"text": "hi"}]}]} """) .when() - .post("/model/" + MAPPED_MODEL_ID + "/converse") + .post("/model/" + MAPPED_MODEL_ID + "/" + operation) .then() .statusCode(408) .body("__type", equalTo("ModelTimeoutException"));