Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Produce quarkus.uuid lazily #45434

Merged
merged 1 commit into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
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 @@ -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();

Comment on lines -77 to -84
Copy link
Member

Choose a reason for hiding this comment

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

Does it actually change something to remove this? Asking because it's the only way to document it in the config.

Copy link
Member

Choose a reason for hiding this comment

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

If a property is declared in a mapping (or the old root), it is always eagerly populated. It wouldn't matter if quarkus.uuid is lazy. This is always trigger the load.

Copy link
Member

Choose a reason for hiding this comment

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

OK. I suppose we need to make sure it's properly documented elsewhere then.

Copy link
Member

Choose a reason for hiding this comment

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

I think we need to do it manually. We don't have a way to do it in the processor. We can ignore properties for documentation, but we can't ignore properties for mapping.

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"));
}
}
Loading