From 8a1ab70291b67ce2134299d28cf2227d9910e219 Mon Sep 17 00:00:00 2001 From: Georgios Andrianakis Date: Wed, 8 Jan 2025 10:27:21 +0200 Subject: [PATCH] Make production of quarkus.uuid lazy This is done because bootstrapping the plumbing needed by the JDK to produce a UUID value is expensive, it thus doesn't make sense to pay this cost when the property isn't actually needed --- .../steps/ConfigGenerationBuildStep.java | 1 - .../java/io/quarkus/runtime/ConfigConfig.java | 8 ---- .../configuration/RuntimeConfigBuilder.java | 38 ++++++++++++++++++- .../it/smallrye/config/QuarkusConfigTest.java | 4 +- 4 files changed, 39 insertions(+), 12 deletions(-) diff --git a/core/deployment/src/main/java/io/quarkus/deployment/steps/ConfigGenerationBuildStep.java b/core/deployment/src/main/java/io/quarkus/deployment/steps/ConfigGenerationBuildStep.java index 6d87ce82fa6f1..090cc485a3af9 100644 --- a/core/deployment/src/main/java/io/quarkus/deployment/steps/ConfigGenerationBuildStep.java +++ b/core/deployment/src/main/java/io/quarkus/deployment/steps/ConfigGenerationBuildStep.java @@ -332,7 +332,6 @@ void generateConfigClass( public void suppressNonRuntimeConfigChanged( BuildProducer suppressNonRuntimeConfigChanged) { suppressNonRuntimeConfigChanged.produce(new SuppressNonRuntimeConfigChangedWarningBuildItem("quarkus.profile")); - suppressNonRuntimeConfigChanged.produce(new SuppressNonRuntimeConfigChangedWarningBuildItem("quarkus.uuid")); suppressNonRuntimeConfigChanged.produce(new SuppressNonRuntimeConfigChangedWarningBuildItem("quarkus.default-locale")); suppressNonRuntimeConfigChanged.produce(new SuppressNonRuntimeConfigChangedWarningBuildItem("quarkus.locales")); suppressNonRuntimeConfigChanged.produce(new SuppressNonRuntimeConfigChangedWarningBuildItem("quarkus.test.arg-line")); diff --git a/core/runtime/src/main/java/io/quarkus/runtime/ConfigConfig.java b/core/runtime/src/main/java/io/quarkus/runtime/ConfigConfig.java index bec67d5c4308a..4e3b4cb626a95 100644 --- a/core/runtime/src/main/java/io/quarkus/runtime/ConfigConfig.java +++ b/core/runtime/src/main/java/io/quarkus/runtime/ConfigConfig.java @@ -74,14 +74,6 @@ public interface ConfigConfig { @WithDefault("warn") BuildTimeMismatchAtRuntime buildTimeMismatchAtRuntime(); - /** - * A property that allows accessing a generated UUID. - * It generates that UUID at startup time. So it changes between two starts including in dev mode. - *
- * Access this generated UUID using expressions: `${quarkus.uuid}`. - */ - Optional uuid(); - enum BuildTimeMismatchAtRuntime { warn, fail diff --git a/core/runtime/src/main/java/io/quarkus/runtime/configuration/RuntimeConfigBuilder.java b/core/runtime/src/main/java/io/quarkus/runtime/configuration/RuntimeConfigBuilder.java index 5415f6e4d521c..5c425918ada1a 100644 --- a/core/runtime/src/main/java/io/quarkus/runtime/configuration/RuntimeConfigBuilder.java +++ b/core/runtime/src/main/java/io/quarkus/runtime/configuration/RuntimeConfigBuilder.java @@ -1,7 +1,10 @@ package io.quarkus.runtime.configuration; +import java.util.Set; import java.util.UUID; +import org.eclipse.microprofile.config.spi.ConfigSource; + import io.smallrye.config.SmallRyeConfigBuilder; import io.smallrye.config.SmallRyeConfigBuilderCustomizer; @@ -12,7 +15,7 @@ public class RuntimeConfigBuilder implements SmallRyeConfigBuilderCustomizer { @Override public void configBuilder(final SmallRyeConfigBuilder builder) { new QuarkusConfigBuilderCustomizer().configBuilder(builder); - builder.withDefaultValue("quarkus.uuid", UUID.randomUUID().toString()); + builder.withSources(new UuiConfigSource()); builder.forClassLoader(Thread.currentThread().getContextClassLoader()) .addDefaultInterceptors() @@ -23,4 +26,37 @@ public void configBuilder(final SmallRyeConfigBuilder builder) { public int priority() { return Integer.MIN_VALUE; } + + private static class UuiConfigSource implements ConfigSource { + + private static final String QUARKUS_UUID = "quarkus.uuid"; + + @Override + public Set getPropertyNames() { + return Set.of(QUARKUS_UUID); + } + + @Override + public String getValue(String propertyName) { + if (propertyName.equals(QUARKUS_UUID)) { + return Holder.UUID_VALUE; + } + return null; + } + + @Override + public String getName() { + return "QuarkusUUIDConfigSource"; + } + + @Override + public int getOrdinal() { + return Integer.MIN_VALUE; + } + + // acts as a lazy value supplier ensuring that the UUID will only be produced when requested + private static class Holder { + private static final String UUID_VALUE = UUID.randomUUID().toString(); + } + } } diff --git a/integration-tests/smallrye-config/src/test/java/io/quarkus/it/smallrye/config/QuarkusConfigTest.java b/integration-tests/smallrye-config/src/test/java/io/quarkus/it/smallrye/config/QuarkusConfigTest.java index 8e60d04ff7bc4..3493c10d5cee3 100644 --- a/integration-tests/smallrye-config/src/test/java/io/quarkus/it/smallrye/config/QuarkusConfigTest.java +++ b/integration-tests/smallrye-config/src/test/java/io/quarkus/it/smallrye/config/QuarkusConfigTest.java @@ -19,13 +19,13 @@ void uuid() { .then() .statusCode(OK.getStatusCode()) .body("value", is(notNullValue())) - .body("configSourceName", equalTo("DefaultValuesConfigSource")); + .body("configSourceName", equalTo("QuarkusUUIDConfigSource")); given() .get("/config/uuid") .then() .statusCode(OK.getStatusCode()) .body("value", is(notNullValue())) - .body("configSourceName", equalTo("DefaultValuesConfigSource")); + .body("configSourceName", equalTo("QuarkusUUIDConfigSource")); } }