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

Fix dev service always restarting on named datasource configuration change #41736

Merged
merged 1 commit into from
Jul 22, 2024
Merged
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
@@ -6,9 +6,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import org.eclipse.microprofile.config.ConfigProvider;
@@ -48,13 +46,9 @@ public class DevServicesDatasourceProcessor {
private static final Logger log = Logger.getLogger(DevServicesDatasourceProcessor.class);
private static final int DOCKER_PS_ID_LENGTH = 12;

// 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");

static volatile List<RunningDevService> databases;

static volatile Map<String, String> cachedProperties;
static volatile Map<String, Object> cachedProperties;

static volatile boolean first = true;

@@ -77,24 +71,9 @@ DevServicesDatasourceResultBuildItem launchDatabases(
//if not and the DB's have already started we just return
if (databases != null) {
boolean restartRequired = false;
if (!restartRequired) {
for (Map.Entry<String, String> entry : cachedProperties.entrySet()) {
if (!Objects.equals(entry.getValue(),
trim(ConfigProvider.getConfig().getOptionalValue(entry.getKey(), String.class).orElse(null)))) {
restartRequired = true;
break;
}
}
}
if (!restartRequired) {
//devservices properties may have been added
for (var name : ConfigProvider.getConfig().getPropertyNames()) {
if (name.startsWith("quarkus.datasource.") && name.contains(".devservices.")
&& !cachedProperties.containsKey(name) && !EXCLUDED_PROPERTIES.contains(name)) {
Malandril marked this conversation as resolved.
Show resolved Hide resolved
restartRequired = true;
break;
}
}
Map<String, Object> newDatasourceConfigs = buildMapFromBuildConfig(dataSourcesBuildTimeConfig);
if (!newDatasourceConfigs.equals(cachedProperties)) {
restartRequired = true;
}
if (!restartRequired) {
for (RunningDevService database : databases) {
@@ -172,18 +151,44 @@ public void run() {
closeBuildItem.addCloseTask(closeTask, true);
}
databases = runningDevServices;
cachedProperties = propertiesMap;
Malandril marked this conversation as resolved.
Show resolved Hide resolved
cachedProperties = buildMapFromBuildConfig(dataSourcesBuildTimeConfig);
for (RunningDevService database : databases) {
devServicesResultBuildItemBuildProducer.produce(database.toBuildItem());
}
return new DevServicesDatasourceResultBuildItem(results);
}

private String trim(String optional) {
if (optional == null) {
return null;
/**
* Returns a map of properties that can trigger a datasource dev service restart if modified.
* It builds this map from the datasource build time config.
*/
private static Map<String, Object> buildMapFromBuildConfig(DataSourcesBuildTimeConfig dataSourcesBuildTimeConfig) {
Map<String, Object> res = new HashMap<>();
for (var datasource : dataSourcesBuildTimeConfig.dataSources().entrySet()) {
String name = datasource.getKey();
DataSourceBuildTimeConfig config = datasource.getValue();
res.put(name + ".db-kind", config.dbKind());
res.put(name + ".db-version", config.dbVersion());
res.put(name + ".devservices.command", config.devservices().command());
res.put(name + ".devservices.container-env", config.devservices().containerEnv());
res.put(name + ".devservices.container-properties.", config.devservices().containerProperties());
res.put(name + ".devservices.db-name", config.devservices().dbName());
res.put(name + ".devservices.image-name", config.devservices().imageName());
res.put(name + ".devservices.init-script-path", config.devservices().initScriptPath());
res.put(name + ".devservices.password", config.devservices().password());
res.put(name + ".devservices.port", config.devservices().port());
res.put(name + ".devservices.properties", config.devservices().properties());
res.put(name + ".devservices.reuse", config.devservices().reuse());
res.put(name + ".devservices.username", config.devservices().username());
res.put(name + ".devservices.volumes", config.devservices().volumes());
Optional<String> username = ConfigUtils.getFirstOptionalValue(
DataSourceUtil.dataSourcePropertyKeys(name, "username"), String.class);
res.put(name + ".username", username);
Optional<String> password = ConfigUtils.getFirstOptionalValue(
DataSourceUtil.dataSourcePropertyKeys(name, "password"), String.class);
res.put(name + ".password", password);
}
return optional.trim();
return res;
}

private RunningDevService startDevDb(