Skip to content

Commit

Permalink
Make production of quarkus.uuid lazy
Browse files Browse the repository at this point in the history
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
  • Loading branch information
geoand committed Jan 9, 2025
1 parent 0d6a99e commit 8a1ab70
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,6 @@ void generateConfigClass(
public void suppressNonRuntimeConfigChanged(
BuildProducer<SuppressNonRuntimeConfigChangedWarningBuildItem> 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"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
* <br>
* Access this generated UUID using expressions: `${quarkus.uuid}`.
*/
Optional<String> uuid();

enum BuildTimeMismatchAtRuntime {
warn,
fail
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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()
Expand All @@ -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<String> 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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
}

0 comments on commit 8a1ab70

Please sign in to comment.