Skip to content

Commit

Permalink
Properly reload BuildTimeRunTime values in Dev mode
Browse files Browse the repository at this point in the history
  • Loading branch information
radcortez committed Jul 20, 2022
1 parent d3b6187 commit 65c2a0f
Show file tree
Hide file tree
Showing 11 changed files with 398 additions and 161 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,9 @@ ReadResult run() {
nameBuilder.setLength(0);
}

allBuildTimeValues.putAll(getDefaults(buildTimePatternMap));
buildTimeRunTimeVisibleValues.putAll(getDefaults(buildTimeRunTimePatternMap));

SmallRyeConfig runtimeDefaultsConfig = getConfigForRuntimeDefaults();
Set<String> registeredRoots = allRoots.stream().map(RootDefinition::getPrefix).collect(toSet());
Set<String> allProperties = getAllProperties(registeredRoots);
Expand Down Expand Up @@ -929,6 +932,45 @@ private Map<String, String> filterActiveProfileProperties(final Map<String, Stri
properties.keySet().removeAll(propertiesToRemove);
return properties;
}

private Map<String, String> getDefaults(final ConfigPatternMap<Container> patternMap) {
Map<String, String> defaultValues = new TreeMap<>();
getDefaults(defaultValues, new StringBuilder(), patternMap);
return defaultValues;
}

private void getDefaults(
final Map<String, String> defaultValues,
final StringBuilder propertyName,
final ConfigPatternMap<Container> patternMap) {

Container matched = patternMap.getMatched();
if (matched != null) {
ClassDefinition.ClassMember member = matched.getClassMember();
assert member instanceof ClassDefinition.ItemMember;
ClassDefinition.ItemMember itemMember = (ClassDefinition.ItemMember) member;
String defaultValue = itemMember.getDefaultValue();
if (defaultValue != null) {
// lookup config to make sure we catch relocates or fallbacks
ConfigValue configValue = config.getConfigValue(propertyName.toString());
if (configValue.getValue() != null) {
defaultValues.put(configValue.getName(), configValue.getValue());
} else {
defaultValues.put(configValue.getName(), defaultValue);
}
}
}

if (propertyName.length() != 0 && patternMap.childNames().iterator().hasNext()) {
propertyName.append(".");
}

for (String childName : patternMap.childNames()) {
getDefaults(defaultValues,
new StringBuilder(propertyName).append(childName.equals(ConfigPatternMap.WILD_CARD) ? "*" : childName),
patternMap.getChild(childName));
}
}
}

public static final class ReadResult {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,18 +89,10 @@ public final class RunTimeConfigurationGenerator {
public static final String CONFIG_CLASS_NAME = "io.quarkus.runtime.generated.Config";
static final String BSDVCS_CLASS_NAME = "io.quarkus.runtime.generated.BootstrapDefaultValuesConfigSource";
static final String RTDVCS_CLASS_NAME = "io.quarkus.runtime.generated.RunTimeDefaultValuesConfigSource";
static final String BTRTDVCS_CLASS_NAME = "io.quarkus.runtime.generated.BuildTimeRunTimeDefaultValuesConfigSource";

// member descriptors

static final MethodDescriptor BTRTDVCS_NEW = MethodDescriptor.ofConstructor(BTRTDVCS_CLASS_NAME);

public static final FieldDescriptor C_INSTANCE = FieldDescriptor.of(CONFIG_CLASS_NAME, "INSTANCE",
CONFIG_CLASS_NAME);
static final FieldDescriptor C_BUILD_TIME_CONFIG_SOURCE = FieldDescriptor.of(CONFIG_CLASS_NAME, "buildTimeConfigSource",
ConfigSource.class);
static final FieldDescriptor C_BUILD_TIME_RUN_TIME_DEFAULTS_CONFIG_SOURCE = FieldDescriptor.of(CONFIG_CLASS_NAME,
"buildTimeRunTimeDefaultsConfigSource", ConfigSource.class);
public static final MethodDescriptor C_CREATE_BOOTSTRAP_CONFIG = MethodDescriptor.ofMethod(CONFIG_CLASS_NAME,
"createBootstrapConfig", CONFIG_CLASS_NAME);
public static final MethodDescriptor C_ENSURE_INITIALIZED = MethodDescriptor.ofMethod(CONFIG_CLASS_NAME,
Expand Down Expand Up @@ -388,29 +380,10 @@ public static final class GenerateOperation implements AutoCloseable {
clinit.invokeStaticMethod(PM_SET_RUNTIME_DEFAULT_PROFILE, clinit.load(ProfileManager.getActiveProfile()));
clinitNameBuilder = clinit.newInstance(SB_NEW);

// create the map for build time config source
final ResultHandle buildTimeValues = clinit.newInstance(HM_NEW);
for (Map.Entry<String, String> entry : buildTimeRunTimeVisibleValues.entrySet()) {
clinit.invokeVirtualMethod(HM_PUT, buildTimeValues, clinit.load(entry.getKey()), clinit.load(entry.getValue()));
}

// static field containing the instance of the class - is set when createBootstrapConfig is run
cc.getFieldCreator(C_INSTANCE)
.setModifiers(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC | Opcodes.ACC_VOLATILE);

// the build time config source field, to feed into the run time config
cc.getFieldCreator(C_BUILD_TIME_CONFIG_SOURCE)
.setModifiers(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC | Opcodes.ACC_FINAL);
final ResultHandle buildTimeConfigSource = clinit.newInstance(PCS_NEW, buildTimeValues,
clinit.load("Build time config"), clinit.load(Integer.MAX_VALUE));
clinit.writeStaticField(C_BUILD_TIME_CONFIG_SOURCE, buildTimeConfigSource);

// the build time run time visible default values config source
cc.getFieldCreator(C_BUILD_TIME_RUN_TIME_DEFAULTS_CONFIG_SOURCE)
.setModifiers(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC | Opcodes.ACC_FINAL);
final ResultHandle buildTimeRunTimeDefaultValuesConfigSource = clinit.newInstance(BTRTDVCS_NEW);
clinit.writeStaticField(C_BUILD_TIME_RUN_TIME_DEFAULTS_CONFIG_SOURCE, buildTimeRunTimeDefaultValuesConfigSource);

// the bootstrap default values config source
if (!buildTimeReadResult.isBootstrapRootsEmpty()) {
cc.getFieldCreator(C_BOOTSTRAP_DEFAULTS_CONFIG_SOURCE)
Expand Down Expand Up @@ -445,13 +418,9 @@ public static final class GenerateOperation implements AutoCloseable {
// the build time config, which is for user use only (not used by us other than for loading converters)
final ResultHandle buildTimeBuilder = clinit.invokeStaticMethod(CU_CONFIG_BUILDER_WITH_ADD_DISCOVERED,
clinit.load(true), clinit.load(false), clinit.load(launchMode));
final ResultHandle array = clinit.newArray(ConfigSource[].class, 3);
// build time values (recorded visible for runtime)
clinit.writeArrayValue(array, 0, buildTimeConfigSource);
// build time runtime defaults for Config Roots
clinit.writeArrayValue(array, 1, buildTimeRunTimeDefaultValuesConfigSource);
final ResultHandle array = clinit.newArray(ConfigSource[].class, 1);
// runtime default values (recorded during build time)
clinit.writeArrayValue(array, 2, clinit.readStaticField(C_SPECIFIED_RUN_TIME_CONFIG_SOURCE));
clinit.writeArrayValue(array, 0, clinit.readStaticField(C_SPECIFIED_RUN_TIME_CONFIG_SOURCE));

clinit.invokeVirtualMethod(SRCB_WITH_SOURCES, buildTimeBuilder, array);
// add safe static sources
Expand Down Expand Up @@ -519,25 +488,9 @@ public void run() {
// at run time (when we're ready) we update the factory and then release the build time config
installConfiguration(clinitConfig, clinit);
if (liveReloadPossible) {
final ResultHandle buildTimeRunTimeDefaultValuesConfigSource = reinit
.readStaticField(C_BUILD_TIME_RUN_TIME_DEFAULTS_CONFIG_SOURCE);
// create the map for build time config source
final ResultHandle buildTimeValues = reinit.newInstance(HM_NEW);
for (Map.Entry<String, String> entry : buildTimeRunTimeVisibleValues.entrySet()) {
reinit.invokeVirtualMethod(HM_PUT, buildTimeValues, reinit.load(entry.getKey()),
reinit.load(entry.getValue()));
}
final ResultHandle buildTimeConfigSource = reinit.newInstance(PCS_NEW, buildTimeValues,
reinit.load("Build time config = Reloaded"), reinit.load(Integer.MAX_VALUE));
// the build time config, which is for user use only (not used by us other than for loading converters)
final ResultHandle buildTimeBuilder = reinit.invokeStaticMethod(CU_CONFIG_BUILDER, reinit.load(true),
reinit.load(launchMode));
final ResultHandle array = reinit.newArray(ConfigSource[].class, 2);
// build time values
reinit.writeArrayValue(array, 0, buildTimeConfigSource);
// build time defaults
reinit.writeArrayValue(array, 1, buildTimeRunTimeDefaultValuesConfigSource);
reinit.invokeVirtualMethod(SRCB_WITH_SOURCES, buildTimeBuilder, array);
// add safe static sources
for (String runtimeConfigSource : staticConfigSources) {
reinit.invokeStaticMethod(CU_ADD_SOURCE_PROVIDER, buildTimeBuilder,
Expand Down Expand Up @@ -620,18 +573,12 @@ public void run() {
// add in the custom sources that bootstrap config needs
ResultHandle bootstrapConfigSourcesArray = null;
if (bootstrapConfigSetupNeeded()) {
bootstrapConfigSourcesArray = readBootstrapConfig.newArray(ConfigSource[].class, 4);
// build time config (expanded values)
readBootstrapConfig.writeArrayValue(bootstrapConfigSourcesArray, 0,
readBootstrapConfig.readStaticField(C_BUILD_TIME_CONFIG_SOURCE));
bootstrapConfigSourcesArray = readBootstrapConfig.newArray(ConfigSource[].class, 2);
// specified run time config default values
readBootstrapConfig.writeArrayValue(bootstrapConfigSourcesArray, 1,
readBootstrapConfig.writeArrayValue(bootstrapConfigSourcesArray, 0,
readBootstrapConfig.readStaticField(C_SPECIFIED_RUN_TIME_CONFIG_SOURCE));
// build time run time visible default config source
readBootstrapConfig.writeArrayValue(bootstrapConfigSourcesArray, 2,
readBootstrapConfig.readStaticField(C_BUILD_TIME_RUN_TIME_DEFAULTS_CONFIG_SOURCE));
// bootstrap config default values
readBootstrapConfig.writeArrayValue(bootstrapConfigSourcesArray, 3,
readBootstrapConfig.writeArrayValue(bootstrapConfigSourcesArray, 1,
readBootstrapConfig.readStaticField(C_BOOTSTRAP_DEFAULTS_CONFIG_SOURCE));

// add bootstrap safe static sources
Expand Down Expand Up @@ -670,21 +617,16 @@ public void run() {

// add in our custom sources
final ResultHandle runtimeConfigSourcesArray = readConfig.newArray(ConfigSource[].class,
bootstrapConfigSetupNeeded() ? 5 : 4);
// build time config (expanded values)
readConfig.writeArrayValue(runtimeConfigSourcesArray, 0, readConfig.readStaticField(C_BUILD_TIME_CONFIG_SOURCE));
bootstrapConfigSetupNeeded() ? 3 : 2);
// specified run time config default values
readConfig.writeArrayValue(runtimeConfigSourcesArray, 1,
readConfig.writeArrayValue(runtimeConfigSourcesArray, 0,
readConfig.readStaticField(C_SPECIFIED_RUN_TIME_CONFIG_SOURCE));
// run time config default values
readConfig.writeArrayValue(runtimeConfigSourcesArray, 2,
readConfig.writeArrayValue(runtimeConfigSourcesArray, 1,
readConfig.readStaticField(C_RUN_TIME_DEFAULTS_CONFIG_SOURCE));
// build time run time visible default config source
readConfig.writeArrayValue(runtimeConfigSourcesArray, 3,
readConfig.readStaticField(C_BUILD_TIME_RUN_TIME_DEFAULTS_CONFIG_SOURCE));
if (bootstrapConfigSetupNeeded()) {
// bootstrap config default values
readConfig.writeArrayValue(runtimeConfigSourcesArray, 4,
readConfig.writeArrayValue(runtimeConfigSourcesArray, 2,
readConfig.readStaticField(C_BOOTSTRAP_DEFAULTS_CONFIG_SOURCE));
}

Expand Down Expand Up @@ -955,9 +897,6 @@ public void run() {

// generate run time default values config source class
generateDefaultValuesConfigSourceClass(runTimePatternMap, RTDVCS_CLASS_NAME);

// generate build time run time visible default values config source class
generateDefaultValuesConfigSourceClass(buildTimeRunTimePatternMap, BTRTDVCS_CLASS_NAME);
}

private void configSweepLoop(MethodDescriptor parserBody, MethodCreator method, ResultHandle config,
Expand Down Expand Up @@ -1741,6 +1680,7 @@ private void reportUnknown(final MethodCreator mc) {
mc.setModifiers(Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC);

ResultHandle unknownProperty = mc.getMethodParam(0);
ResultHandle unknown = mc.getMethodParam(1);

// Ignore all build property names. This is to ignore any properties mapped with @ConfigMapping, because
// these do not fall into the ignore ConfigPattern.
Expand All @@ -1751,20 +1691,8 @@ private void reportUnknown(final MethodCreator mc) {
mc.ifTrue(equalsResult).trueBranch().returnValue(null);
}

ResultHandle unknown = mc.getMethodParam(1);

// Ignore recorded properties. If properties are present here, it means that they were recorded at build
// time as being mapped in a @ConfigMapping
ResultHandle buildTimeRunTimeSource = mc.readStaticField(C_BUILD_TIME_CONFIG_SOURCE);
ResultHandle buildTimeRunTimeValue = mc.invokeInterfaceMethod(CS_GET_VALUE, buildTimeRunTimeSource,
unknownProperty);
mc.ifNotNull(buildTimeRunTimeValue).trueBranch().returnValue(null);

ResultHandle buildTimeRunTimeDefaultsSource = mc.readStaticField(C_BUILD_TIME_RUN_TIME_DEFAULTS_CONFIG_SOURCE);
ResultHandle buildTimeRunTimeDefaultValue = mc.invokeInterfaceMethod(CS_GET_VALUE, buildTimeRunTimeDefaultsSource,
unknownProperty);
mc.ifNotNull(buildTimeRunTimeDefaultValue).trueBranch().returnValue(null);

ResultHandle runtimeSpecifiedSource = mc.readStaticField(C_SPECIFIED_RUN_TIME_CONFIG_SOURCE);
ResultHandle runtimeSpecifiedValue = mc.invokeInterfaceMethod(CS_GET_VALUE, runtimeSpecifiedSource,
unknownProperty);
Expand Down
Loading

0 comments on commit 65c2a0f

Please sign in to comment.