Skip to content

Commit

Permalink
Only cache a subset of properties in datasource restart cache
Browse files Browse the repository at this point in the history
  • Loading branch information
Malandril committed Jul 14, 2024
1 parent 80627ae commit 6a7e471
Showing 1 changed file with 57 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
Expand Down Expand Up @@ -51,7 +52,25 @@ public class DevServicesDatasourceProcessor {

// list of devservices properties we should not check for restart
// see issue #30390
private static final Set<String> EXCLUDED_PROPERTIES = Set.of("quarkus.datasource.devservices.enabled");
private static final Set<String> EXCLUDED_RESTART_PROPERTIES = Set.of();

/**
* Properties that if modified will trigger a datasource dev service restart
*/
private static final Set<String> RESTART_TRIGGERING_PROPERTIES = Set.of("db-kind",
"db-version",
"devservices.command",
"devservices.container-env.",
"devservices.container-properties.",
"devservices.db-name",
"devservices.image-name",
"devservices.init-script-path",
"devservices.port",
"devservices.properties.",
"devservices.reuse",
"devservices.volumes.",
"password",
"username");

static volatile List<RunningDevService> databases;

Expand All @@ -78,14 +97,10 @@ DevServicesDatasourceResultBuildItem launchDatabases(
//if not and the DB's have already started we just return
if (databases != null) {
boolean restartRequired = false;
Map<String, String> newDatasourceConfigs = ConfigProvider.getConfig()
.unwrap(SmallRyeConfig.class)
.getOptionalValues("quarkus.datasource",
String.class, String.class)
.orElse(Map.of());
Map<String, String> newDatasourceConfigs = getRestartTriggeringPropertiesValues();
for (Map.Entry<String, String> entry : cachedProperties.entrySet()) {
String newValue = newDatasourceConfigs.get(entry.getKey());
if (!Objects.equals(entry.getValue(), trim(newValue))) {
if (!Objects.equals(entry.getValue(), newValue)) {
restartRequired = true;
break;
}
Expand Down Expand Up @@ -174,17 +189,47 @@ public void run() {
closeBuildItem.addCloseTask(closeTask, true);
}
databases = runningDevServices;
cachedProperties = ConfigProvider.getConfig()
.unwrap(SmallRyeConfig.class)
.getOptionalValues("quarkus.datasource", String.class, String.class)
.orElse(Map.of());
cachedProperties = getRestartTriggeringPropertiesValues();
for (RunningDevService database : databases) {
devServicesResultBuildItemBuildProducer.produce(database.toBuildItem());
}
return new DevServicesDatasourceResultBuildItem(results);
}

private String trim(String optional) {
/**
* Returns a map of properties that are can trigger a datasource dev service restart if modified.
* It excludes EXCLUDED_PROPERTIES and includes only RESTART_TRIGGERING_PROPERTIES.
* If a property ends with {@code .} then it will also trigger a restart on any sub properties change
*/
private static Map<String, String> getRestartTriggeringPropertiesValues() {
Map<String, String> datasourceProperties;
try {
datasourceProperties = ConfigProvider.getConfig()
.unwrap(SmallRyeConfig.class)
.getOptionalValues("quarkus.datasource",
String.class,
String.class)
.orElse(Map.of());
} catch (NoSuchElementException e) {
log.error("Could not get datasource properties", e);
datasourceProperties = Map.of();
}
Map<String, String> filteredProperties = new HashMap<>();
for (var entry : datasourceProperties.entrySet()) {
if (!EXCLUDED_RESTART_PROPERTIES.contains(entry.getKey())) {
for (String property : RESTART_TRIGGERING_PROPERTIES) {
if (property.endsWith(".") && entry.getKey().contains(property) || entry.getKey()
.endsWith(property)) {
filteredProperties.put(entry.getKey(), trim(entry.getValue()));
break;
}
}
}
}
return filteredProperties;
}

private static String trim(String optional) {
if (optional == null) {
return null;
}
Expand Down

0 comments on commit 6a7e471

Please sign in to comment.