Skip to content

Commit

Permalink
Merge pull request #41815 from xstefank/context-health-mdc-i41746
Browse files Browse the repository at this point in the history
Create new vertx context for blocking health checks
  • Loading branch information
geoand authored Jul 11, 2024
2 parents bf2e945 + d5d0aee commit 0109749
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package io.quarkus.smallrye.health.test;

import static org.hamcrest.Matchers.is;

import org.eclipse.microprofile.health.HealthCheck;
import org.eclipse.microprofile.health.HealthCheckResponse;
import org.eclipse.microprofile.health.Liveness;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusUnitTest;
import io.restassured.RestAssured;
import io.restassured.parsing.Parser;
import io.vertx.core.Context;
import io.vertx.core.Vertx;

class BlockingChecksVertxContextDuplicationTest {

@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest()
.withApplicationRoot((jar) -> jar
.addClasses(ContextCaptureCheck1.class, ContextCaptureCheck2.class)
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml"));

@Test
void testBlockingChecksPropagateVertxContext() {
try {
RestAssured.defaultParser = Parser.JSON;
RestAssured.when().get("/q/health").then()
.body("status", is("UP"),
"checks.size()", is(2));

Assertions.assertNotEquals(ContextCaptureCheck1.capturedContext, ContextCaptureCheck2.capturedContext,
"Expected different contexts to be propagated into different blocking health checks");
} finally {
RestAssured.reset();
}
}

@Liveness
public static class ContextCaptureCheck1 implements HealthCheck {

public static Context capturedContext = null;

@Override
public HealthCheckResponse call() {
capturedContext = Vertx.currentContext();
return HealthCheckResponse.up("ContextCaptureCheck1");
}
}

@Liveness
public static class ContextCaptureCheck2 implements HealthCheck {

public static Context capturedContext = null;

@Override
public HealthCheckResponse call() {
capturedContext = Vertx.currentContext();
return HealthCheckResponse.up("ContextCaptureCheck2");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package io.quarkus.smallrye.health.runtime;

import java.util.concurrent.Callable;
import java.util.concurrent.Executor;

import jakarta.inject.Singleton;

import org.eclipse.microprofile.health.HealthCheck;
import org.eclipse.microprofile.health.HealthCheckResponse;

import io.smallrye.common.vertx.VertxContext;
import io.smallrye.health.AsyncHealthCheckFactory;
import io.smallrye.health.api.AsyncHealthCheck;
import io.smallrye.mutiny.Uni;
import io.smallrye.mutiny.vertx.MutinyHelper;
import io.vertx.core.Context;
import io.vertx.core.Vertx;

/**
Expand All @@ -27,7 +32,19 @@ public QuarkusAsyncHealthCheckFactory(Vertx vertx) {
@Override
public Uni<HealthCheckResponse> callSync(HealthCheck healthCheck) {
Uni<HealthCheckResponse> healthCheckResponseUni = super.callSync(healthCheck);
return healthCheckResponseUni.runSubscriptionOn(MutinyHelper.blockingExecutor(vertx, false));
return healthCheckResponseUni.runSubscriptionOn(new Executor() {
@Override
public void execute(Runnable command) {
Context duplicatedContext = VertxContext.createNewDuplicatedContext(vertx.getOrCreateContext());
duplicatedContext.executeBlocking(new Callable<Void>() {
@Override
public Void call() throws Exception {
command.run();
return null;
}
}, false);
}
});
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private void doHandle(RoutingContext ctx, ManagedContext requestContext) {
.set(HttpHeaders.CONTENT_TYPE, "application/json; charset=UTF-8")
.set(HttpHeaders.CACHE_CONTROL, "no-store");
Buffer buffer = Buffer.buffer(256); // this size seems to cover the basic health checks
try (BufferOutputStream outputStream = new BufferOutputStream(buffer);) {
try (BufferOutputStream outputStream = new BufferOutputStream(buffer)) {
reporter.reportHealth(outputStream, health);
resp.end(buffer);
} catch (IOException e) {
Expand Down

0 comments on commit 0109749

Please sign in to comment.