From de440ade0f361eb89b4045721cde6722e6467a73 Mon Sep 17 00:00:00 2001 From: Ozan Gunalp Date: Tue, 17 Oct 2023 18:21:18 +0200 Subject: [PATCH] Reset the Apicurio registry client in dev mode and tests It holds a reference to Vertx, which will be closed on hot reload Fixes #26557 --- .../ApicurioRegistryClientProcessor.java | 6 ++++- ...curioRegistryInternalsExpectationTest.java | 13 +++++++++++ .../common/ApicurioRegistryClient.java | 22 ++++++++++++++++++- 3 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 extensions/schema-registry/apicurio/common/deployment/src/test/java/io/quarkus/apicurio/registry/common/ApicurioRegistryInternalsExpectationTest.java diff --git a/extensions/schema-registry/apicurio/common/deployment/src/main/java/io/quarkus/apicurio/registry/common/ApicurioRegistryClientProcessor.java b/extensions/schema-registry/apicurio/common/deployment/src/main/java/io/quarkus/apicurio/registry/common/ApicurioRegistryClientProcessor.java index f031b1f1c2a15..bb74ef643ac1c 100644 --- a/extensions/schema-registry/apicurio/common/deployment/src/main/java/io/quarkus/apicurio/registry/common/ApicurioRegistryClientProcessor.java +++ b/extensions/schema-registry/apicurio/common/deployment/src/main/java/io/quarkus/apicurio/registry/common/ApicurioRegistryClientProcessor.java @@ -8,6 +8,7 @@ import io.quarkus.deployment.annotations.ExecutionTime; import io.quarkus.deployment.annotations.Record; import io.quarkus.deployment.builditem.ExtensionSslNativeSupportBuildItem; +import io.quarkus.deployment.builditem.LaunchModeBuildItem; import io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem; import io.quarkus.deployment.builditem.nativeimage.ServiceProviderBuildItem; import io.quarkus.smallrye.openapi.deployment.spi.IgnoreStaticDocumentBuildItem; @@ -48,7 +49,10 @@ void ignoreIncludedOpenAPIDocument(BuildProducer @BuildStep @Record(ExecutionTime.RUNTIME_INIT) - public void apicurioRegistryClient(VertxBuildItem vertx, ApicurioRegistryClient client) { + public void apicurioRegistryClient(VertxBuildItem vertx, ApicurioRegistryClient client, LaunchModeBuildItem launchMode) { + if (launchMode.getLaunchMode().isDevOrTest()) { + client.clearHttpClient(); + } client.setup(vertx.getVertx()); } diff --git a/extensions/schema-registry/apicurio/common/deployment/src/test/java/io/quarkus/apicurio/registry/common/ApicurioRegistryInternalsExpectationTest.java b/extensions/schema-registry/apicurio/common/deployment/src/test/java/io/quarkus/apicurio/registry/common/ApicurioRegistryInternalsExpectationTest.java new file mode 100644 index 0000000000000..ab2da367a32d8 --- /dev/null +++ b/extensions/schema-registry/apicurio/common/deployment/src/test/java/io/quarkus/apicurio/registry/common/ApicurioRegistryInternalsExpectationTest.java @@ -0,0 +1,13 @@ +package io.quarkus.apicurio.registry.common; + +import org.junit.jupiter.api.Test; + +import io.apicurio.rest.client.spi.ApicurioHttpClientFactory; + +public class ApicurioRegistryInternalsExpectationTest { + @Test + public void test() throws NoSuchFieldException { + // we need this to reset the client in continuous testing + ApicurioHttpClientFactory.class.getDeclaredField("providerReference"); + } +} diff --git a/extensions/schema-registry/apicurio/common/runtime/src/main/java/io/quarkus/apicurio/registry/common/ApicurioRegistryClient.java b/extensions/schema-registry/apicurio/common/runtime/src/main/java/io/quarkus/apicurio/registry/common/ApicurioRegistryClient.java index 1a072229c2fcd..80a544a63979c 100644 --- a/extensions/schema-registry/apicurio/common/runtime/src/main/java/io/quarkus/apicurio/registry/common/ApicurioRegistryClient.java +++ b/extensions/schema-registry/apicurio/common/runtime/src/main/java/io/quarkus/apicurio/registry/common/ApicurioRegistryClient.java @@ -1,14 +1,34 @@ package io.quarkus.apicurio.registry.common; +import java.lang.reflect.Field; +import java.util.concurrent.atomic.AtomicReference; + +import org.jboss.logging.Logger; + import io.apicurio.registry.rest.client.RegistryClientFactory; import io.apicurio.rest.client.VertxHttpClientProvider; +import io.apicurio.rest.client.spi.ApicurioHttpClientFactory; import io.quarkus.runtime.RuntimeValue; import io.quarkus.runtime.annotations.Recorder; import io.vertx.core.Vertx; @Recorder public class ApicurioRegistryClient { + + private static final Logger log = Logger.getLogger(ApicurioRegistryClient.class); + public void setup(RuntimeValue vertx) { RegistryClientFactory.setProvider(new VertxHttpClientProvider(vertx.getValue())); } -} + + public void clearHttpClient() { + try { + Field providerReference = ApicurioHttpClientFactory.class.getDeclaredField("providerReference"); + providerReference.setAccessible(true); + AtomicReference ref = (AtomicReference) providerReference.get(null); + ref.set(null); + } catch (NoSuchFieldException | IllegalAccessException t) { + log.error("Failed to clear Apicurio Http Client provider", t); + } + } +} \ No newline at end of file