Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@
import com.linecorp.armeria.common.HttpResponse;
import com.linecorp.armeria.common.HttpStatus;
import com.linecorp.armeria.common.util.BlockingTaskExecutor;
import com.linecorp.armeria.common.util.TimeoutMode;
import com.linecorp.armeria.server.DecoratingHttpServiceFunction;
import com.linecorp.armeria.server.Server;
import com.linecorp.armeria.server.ServiceRequestContext;
import com.linecorp.armeria.server.annotation.Get;
import io.unitycatalog.server.service.UnityCatalogRestService;
import io.unitycatalog.server.utils.ServerProperties;
import io.unitycatalog.server.utils.ServerProperties.Property;
import java.net.ServerSocket;
import java.time.Duration;
import java.util.Properties;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;
Expand All @@ -35,6 +39,20 @@
*/
class EventLoopStallTest {

/**
* Upper bound for things that should happen: a request reaching its handler, a response, an
* executor task. Passing runs return as soon as they do, so the bound only matters on a slow
* machine. The timings that are themselves assertions, like the 3s window in which {@code stop()}
* must still be waiting, stay fixed.
*/
private static final long WAIT_SECONDS = 30;

/**
* The request timeout of a held request, on the server and in the client: long enough for a
* request held through every wait in a test, so neither side gives up first.
*/
private static final Duration REQUEST_TIMEOUT = Duration.ofSeconds(3 * WAIT_SECONDS);

@Test
void secondStreamStartsWhileFirstIsBlocked() throws Exception {
CountDownLatch entered = new CountDownLatch(1);
Expand All @@ -44,8 +62,7 @@ void secondStreamStartsWhileFirstIsBlocked() throws Exception {
DecoratingHttpServiceFunction blockingAuth =
(delegate, ctx, req) -> {
if (req.path().endsWith("/hold")) {
entered.countDown();
release.await();
hold(ctx, entered, release);
}
return delegate.serve(ctx, req);
};
Expand Down Expand Up @@ -95,6 +112,10 @@ void secondStreamStartsWhileHandlerIsBlocked() throws Exception {

@Test
void stopWaitsForBlockedRequestAndExecutorRemainsActive() throws Exception {
// Long enough to outlast the waits below, unlike the default of 5s: stop() must still be
// waiting for the held request when it is released.
Properties properties = new Properties();
properties.setProperty(Property.SHUTDOWN_TIMEOUT.getKey(), "PT" + WAIT_SECONDS + "S");
CountDownLatch entered = new CountDownLatch(1);
CountDownLatch release = new CountDownLatch(1);
BlockingTaskExecutor blockingTaskExecutor =
Expand All @@ -104,7 +125,7 @@ void stopWaitsForBlockedRequestAndExecutorRemainsActive() throws Exception {
findAvailablePort(),
"/api/",
"/control/",
new ServerProperties(new Properties()),
new ServerProperties(properties),
blockingTaskExecutor);
builder.annotate("probe", new BlockingProbe(entered, release, new CountDownLatch(1)));

Expand All @@ -113,21 +134,23 @@ void stopWaitsForBlockedRequestAndExecutorRemainsActive() throws Exception {
server.start().join();
int port = server.activeLocalPort();
WebClient client =
WebClient.builder("h2c://127.0.0.1:" + port).responseTimeoutMillis(5_000).build();
WebClient.builder("h2c://127.0.0.1:" + port)
.responseTimeoutMillis(REQUEST_TIMEOUT.toMillis())
.build();

CompletableFuture<AggregatedHttpResponse> response =
client.get("/api/probe/hold").aggregate();
assertThat(entered.await(5, TimeUnit.SECONDS)).isTrue();
assertThat(entered.await(WAIT_SECONDS, TimeUnit.SECONDS)).isTrue();

// Armeria spends about 2s in its own quiet period even without draining, so wait past that
// (but within the 5s shutdown timeout) before releasing: a stop that does not drain has
// closed the connection by then and the response below fails.
// (but within the shutdown timeout set above) before releasing: a stop that does not drain
// has closed the connection by then and the response below fails.
CompletableFuture<Void> stopping = server.stop();
assertThatThrownBy(() -> stopping.get(3, TimeUnit.SECONDS))
.isInstanceOf(TimeoutException.class);

release.countDown();
assertThat(response.get(5, TimeUnit.SECONDS).status()).isEqualTo(HttpStatus.OK);
assertThat(response.get(WAIT_SECONDS, TimeUnit.SECONDS).status()).isEqualTo(HttpStatus.OK);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

waiting 30s for the response here won't help if the server still has its default 5s shutdown timeout. after stop() and the 3s wait there's only ~2s left before shutdown aborts the request, so could we set Property.SHUTDOWN_TIMEOUT explicitly in this test like UnityCatalogServerShutdownTest does?

+    Properties properties = new Properties();
+    properties.setProperty(
+        Property.SHUTDOWN_TIMEOUT.getKey(), "PT" + WAIT_SECONDS + "S");
     ArmeriaServerBuilder builder =
         new ArmeriaServerBuilder(
             findAvailablePort(),
             "/api/",
             "/control/",
-            new ServerProperties(new Properties()),
+            new ServerProperties(properties),
             blockingTaskExecutor);

stopping.join();

assertThat(blockingTaskExecutor.submit(() -> "still active").get()).isEqualTo("still active");
Expand All @@ -151,8 +174,7 @@ void requestCancelledWhileQueuedDoesNotRunAuthOrHandler() throws Exception {
DecoratingHttpServiceFunction blockingAuth =
(delegate, ctx, req) -> {
if (req.path().endsWith("/hold")) {
entered.countDown();
release.await();
hold(ctx, entered, release);
} else {
fastAuthCalls.incrementAndGet();
}
Expand Down Expand Up @@ -180,10 +202,10 @@ void requestCancelledWhileQueuedDoesNotRunAuthOrHandler() throws Exception {
// their frames in order.
WebClient client =
WebClient.builder("h2c://127.0.0.1:" + server.activeLocalPort())
.responseTimeoutMillis(5_000)
.responseTimeoutMillis(REQUEST_TIMEOUT.toMillis())
.build();
CompletableFuture<AggregatedHttpResponse> held = client.get("/api/probe/hold").aggregate();
assertThat(entered.await(5, TimeUnit.SECONDS)).isTrue();
assertThat(entered.await(WAIT_SECONDS, TimeUnit.SECONDS)).isTrue();

// The client gives up while the request is still queued and resets the stream, which
// cancels the request on the server.
Expand All @@ -193,17 +215,17 @@ void requestCancelledWhileQueuedDoesNotRunAuthOrHandler() throws Exception {
HttpRequest.of(HttpMethod.GET, "/api/probe/fast"),
RequestOptions.builder().responseTimeoutMillis(300).build())
.aggregate();
assertThatThrownBy(() -> queued.get(5, TimeUnit.SECONDS))
assertThatThrownBy(() -> queued.get(WAIT_SECONDS, TimeUnit.SECONDS))
.isInstanceOf(ExecutionException.class);
// Answered on the event loop without the blocking pool, so once it returns the server has
// processed the reset sent before it.
assertThat(client.get("/unrouted").aggregate().get(5, TimeUnit.SECONDS).status())
assertThat(client.get("/unrouted").aggregate().get(WAIT_SECONDS, TimeUnit.SECONDS).status())
.isEqualTo(HttpStatus.NOT_FOUND);

release.countDown();
assertThat(held.get(5, TimeUnit.SECONDS).status()).isEqualTo(HttpStatus.OK);
assertThat(held.get(WAIT_SECONDS, TimeUnit.SECONDS).status()).isEqualTo(HttpStatus.OK);
// Runs after the queued task on the single thread, so that task has finished.
assertThat(blockingTaskExecutor.submit(() -> "drained").get(5, TimeUnit.SECONDS))
assertThat(blockingTaskExecutor.submit(() -> "drained").get(WAIT_SECONDS, TimeUnit.SECONDS))
.isEqualTo("drained");
assertThat(fastAuthCalls).hasValue(0);
assertThat(fastEntered.getCount()).isEqualTo(1);
Expand All @@ -217,6 +239,22 @@ void requestCancelledWhileQueuedDoesNotRunAuthOrHandler() throws Exception {
}
}

/**
* Marks the request as entered, then holds it until {@code release}. Raises the request's own
* timeout first: Armeria's default of 10s would otherwise fail a request held that long.
*
* @param ctx the held request's context
* @param entered counted down once the request is held
* @param release awaited before the request continues
*/
private static void hold(
ServiceRequestContext ctx, CountDownLatch entered, CountDownLatch release)
throws InterruptedException {
ctx.setRequestTimeout(TimeoutMode.SET_FROM_START, REQUEST_TIMEOUT);
entered.countDown();
release.await();
}

// The API surface is a port-based virtual host, which needs a concrete port rather than 0.
private static int findAvailablePort() throws Exception {
try (ServerSocket socket = new ServerSocket(0)) {
Expand All @@ -238,15 +276,17 @@ private static void assertSecondStreamOverlaps(
// h2c prior knowledge, one connection: both calls are streams on the same channel, which is
// what a single JDK HttpClient does against this server.
WebClient client =
WebClient.builder("h2c://127.0.0.1:" + port).responseTimeoutMillis(5_000).build();
WebClient.builder("h2c://127.0.0.1:" + port)
.responseTimeoutMillis(REQUEST_TIMEOUT.toMillis())
.build();

CompletableFuture<AggregatedHttpResponse> held = client.get("/api/probe/hold").aggregate();
assertThat(entered.await(5, TimeUnit.SECONDS))
assertThat(entered.await(WAIT_SECONDS, TimeUnit.SECONDS))
.as("the first stream reaches %s", blockedOn)
.isTrue();

CompletableFuture<AggregatedHttpResponse> fast = client.get("/api/probe/fast").aggregate();
boolean overlapped = fastEntered.await(2, TimeUnit.SECONDS);
boolean overlapped = fastEntered.await(WAIT_SECONDS, TimeUnit.SECONDS);
release.countDown();

assertThat(overlapped)
Expand Down Expand Up @@ -301,8 +341,7 @@ private BlockingProbe(

@Get("/hold")
public HttpResponse hold() throws InterruptedException {
entered.countDown();
release.await();
EventLoopStallTest.hold(ServiceRequestContext.current(), entered, release);
return HttpResponse.of("hold");
}

Expand Down
Loading