Skip to content

Commit

Permalink
Dev services cleanup
Browse files Browse the repository at this point in the history
- Don't restart in test mode, it slows down continuous testing
- Processors only produce DevServicesConfigResultBuildItem, quarkus
  handles turning it into runtime config
- If no changes are made Quarkus handles the cached config, so the
  processor does not have to

Fixes #19044
  • Loading branch information
stuartwdouglas committed Jul 29, 2021
1 parent b5f2637 commit 64781be
Show file tree
Hide file tree
Showing 17 changed files with 178 additions and 190 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.quarkus.deployment.builditem;

import io.quarkus.builder.item.MultiBuildItem;

/**
* Configuration property that is the result of start dev services.
*
* Used to start and configure dev services, any processor starting dev services should produce these items.
*
* Quarkus will make sure the relevant settings are present in both JVM and native modes.
*/
public final class DevServicesConfigResultBuildItem extends MultiBuildItem {

final String key;
final String value;

public DevServicesConfigResultBuildItem(String key, String value) {
this.key = key;
this.value = value;
}

public String getKey() {
return key;
}

public String getValue() {
return value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.quarkus.deployment.builditem;

import java.util.Map;

import io.quarkus.builder.item.SimpleBuildItem;

/**
* Build item that contains the final results of all
*/
public final class DevServicesLauncherConfigResultBuildItem extends SimpleBuildItem {

final Map<String, String> config;

public DevServicesLauncherConfigResultBuildItem(Map<String, String> config) {
this.config = config;
}

public Map<String, String> getConfig() {
return config;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
import io.quarkus.builder.item.MultiBuildItem;

/**
* Configuration property that is the result of start dev services.
*
* Used to start and configure dev services in native mode.
* see {@link DevServicesConfigResultBuildItem} and {@link DevServicesLauncherConfigResultBuildItem}
*/
@Deprecated
public final class DevServicesNativeConfigResultBuildItem extends MultiBuildItem {

final String key;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package io.quarkus.deployment.steps;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.ConfigProvider;

import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.annotations.Produce;
import io.quarkus.deployment.builditem.DevServicesConfigResultBuildItem;
import io.quarkus.deployment.builditem.DevServicesLauncherConfigResultBuildItem;
import io.quarkus.deployment.builditem.DevServicesNativeConfigResultBuildItem;
import io.quarkus.deployment.builditem.RunTimeConfigurationDefaultBuildItem;
import io.quarkus.deployment.builditem.ServiceStartBuildItem;

class DevServicesConfigBuildStep {

private static volatile Map<String, String> oldProperties = Collections.emptyMap();

@BuildStep
List<DevServicesConfigResultBuildItem> deprecated(List<DevServicesNativeConfigResultBuildItem> items) {
return items.stream().map(s -> new DevServicesConfigResultBuildItem(s.getKey(), s.getValue()))
.collect(Collectors.toList());
}

@BuildStep
@Produce(ServiceStartBuildItem.class)
DevServicesLauncherConfigResultBuildItem setup(BuildProducer<RunTimeConfigurationDefaultBuildItem> runtimeConfig,
List<DevServicesConfigResultBuildItem> devServicesConfigResultBuildItems) {
Map<String, String> newProperties = new HashMap<>(devServicesConfigResultBuildItems.stream().collect(
Collectors.toMap(DevServicesConfigResultBuildItem::getKey, DevServicesConfigResultBuildItem::getValue)));
Config config = ConfigProvider.getConfig();
//check if there are existing already started dev services
//if there were no changes to the processors they don't produce config
//so we merge existing config from previous runs
//we also check the current config, as the dev service may have been disabled by explicit config
for (Map.Entry<String, String> entry : oldProperties.entrySet()) {
if (!newProperties.containsKey(entry.getKey()) && config.getOptionalValue(entry.getKey(), String.class).isEmpty()) {
newProperties.put(entry.getKey(), entry.getValue());
}
}
for (Map.Entry<String, String> entry : newProperties.entrySet()) {
runtimeConfig.produce(new RunTimeConfigurationDefaultBuildItem(entry.getKey(), entry.getValue()));
}
oldProperties = newProperties;
return new DevServicesLauncherConfigResultBuildItem(Collections.unmodifiableMap(newProperties));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import io.quarkus.deployment.annotations.ExecutionTime;
import io.quarkus.deployment.annotations.Produce;
import io.quarkus.deployment.annotations.Record;
import io.quarkus.deployment.builditem.DevServicesNativeConfigResultBuildItem;
import io.quarkus.deployment.builditem.DevServicesConfigResultBuildItem;
import io.quarkus.deployment.builditem.FeatureBuildItem;
import io.quarkus.extest.runtime.classpath.ClasspathEntriesRecorder;
import io.quarkus.extest.runtime.classpath.RecordedClasspathEntries;
Expand All @@ -39,7 +39,7 @@ void registerRecordedClasspathEntries(ClasspathEntriesRecorder classpathEntriesR
@Produce(FeatureBuildItem.class)
// This makes sure we execute this in io.quarkus.test.junit.IntegrationTestUtil.handleDevDb,
// so that we can reproduce a problem that happens in the Hibernate ORM extension.
@Produce(DevServicesNativeConfigResultBuildItem.class)
@Produce(DevServicesConfigResultBuildItem.class)
void recordDuringAugmentation(TestBuildTimeConfig config)
throws IOException {
List<String> resourcesToRecord = getResourcesToRecord(config);
Expand All @@ -54,7 +54,7 @@ void recordDuringAugmentation(TestBuildTimeConfig config)
@Record(ExecutionTime.STATIC_INIT)
// This makes sure we execute this in io.quarkus.test.junit.IntegrationTestUtil.handleDevDb,
// so that we can reproduce a problem that happens in the Hibernate ORM extension.
@Produce(DevServicesNativeConfigResultBuildItem.class)
@Produce(DevServicesConfigResultBuildItem.class)
void recordDuringStaticInit(ClasspathEntriesRecorder classpathEntriesRecorder, TestBuildTimeConfig config)
throws IOException {
List<String> resourcesToRecord = getResourcesToRecord(config);
Expand All @@ -69,7 +69,7 @@ void recordDuringStaticInit(ClasspathEntriesRecorder classpathEntriesRecorder, T
@Record(ExecutionTime.RUNTIME_INIT)
// This makes sure we execute this in io.quarkus.test.junit.IntegrationTestUtil.handleDevDb,
// so that we can reproduce a problem that happens in the Hibernate ORM extension.
@Produce(DevServicesNativeConfigResultBuildItem.class)
@Produce(DevServicesConfigResultBuildItem.class)
void recordDuringRuntimeInit(ClasspathEntriesRecorder classpathEntriesRecorder, TestBuildTimeConfig config)
throws IOException {
List<String> resourcesToRecord = getResourcesToRecord(config);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@
import io.quarkus.deployment.IsNormal;
import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.builditem.DevServicesNativeConfigResultBuildItem;
import io.quarkus.deployment.builditem.LaunchModeBuildItem;
import io.quarkus.deployment.builditem.RunTimeConfigurationDefaultBuildItem;
import io.quarkus.runtime.LaunchMode;
import io.quarkus.deployment.builditem.DevServicesConfigResultBuildItem;
import io.quarkus.runtime.configuration.ConfigUtils;

/**
Expand All @@ -39,19 +36,13 @@ public class DevServicesApicurioRegistryProcessor {
private final IsDockerWorking isDockerWorking = new IsDockerWorking(true);

@BuildStep(onlyIfNot = IsNormal.class)
public void startApicurioRegistryDevService(
LaunchModeBuildItem launchMode,
ApicurioRegistryDevServicesBuildTimeConfig apicurioRegistryDevServices,
BuildProducer<RunTimeConfigurationDefaultBuildItem> runTimeConfiguration,
BuildProducer<DevServicesNativeConfigResultBuildItem> devServicesConfiguration) {
public void startApicurioRegistryDevService(ApicurioRegistryDevServicesBuildTimeConfig apicurioRegistryDevServices,
BuildProducer<DevServicesConfigResultBuildItem> devServicesConfiguration) {

ApicurioRegistryDevServiceCfg configuration = getConfiguration(apicurioRegistryDevServices);

if (closeable != null) {
boolean restartRequired = launchMode.getLaunchMode() == LaunchMode.TEST;
if (!restartRequired) {
restartRequired = !configuration.equals(cfg);
}
boolean restartRequired = !configuration.equals(cfg);
if (!restartRequired) {
return;
}
Expand All @@ -67,9 +58,7 @@ public void startApicurioRegistryDevService(
cfg = configuration;
closeable = apicurioRegistry.getCloseable();

runTimeConfiguration.produce(new RunTimeConfigurationDefaultBuildItem(
REGISTRY_URL_CONFIG, apicurioRegistry.getUrl() + "/apis/registry/v2"));
devServicesConfiguration.produce(new DevServicesNativeConfigResultBuildItem(
devServicesConfiguration.produce(new DevServicesConfigResultBuildItem(
REGISTRY_URL_CONFIG, apicurioRegistry.getUrl() + "/apis/registry/v2"));

log.infof("Dev Services for Apicurio Registry started. The registry is available at %s", apicurioRegistry.getUrl());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,8 @@
import io.quarkus.deployment.IsNormal;
import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.builditem.DevServicesNativeConfigResultBuildItem;
import io.quarkus.deployment.builditem.DevServicesConfigResultBuildItem;
import io.quarkus.deployment.builditem.LaunchModeBuildItem;
import io.quarkus.deployment.builditem.RunTimeConfigurationDefaultBuildItem;
import io.quarkus.deployment.builditem.ServiceStartBuildItem;
import io.quarkus.deployment.pkg.builditem.CurateOutcomeBuildItem;
import io.quarkus.runtime.LaunchMode;

Expand All @@ -40,8 +38,6 @@ public class DevServicesDatasourceProcessor {

static volatile Map<String, String> cachedProperties;

static volatile List<RunTimeConfigurationDefaultBuildItem> databaseConfig;

static volatile boolean first = true;

private final IsDockerWorking isDockerWorking = new IsDockerWorking(true);
Expand All @@ -52,10 +48,8 @@ DevServicesDatasourceResultBuildItem launchDatabases(CurateOutcomeBuildItem cura
List<DevServicesDatasourceProviderBuildItem> devDBProviders,
DataSourcesBuildTimeConfig dataSourceBuildTimeConfig,
LaunchModeBuildItem launchMode,
BuildProducer<RunTimeConfigurationDefaultBuildItem> runTimeConfigurationDefaultBuildItemBuildProducer,
List<DevServicesDatasourceConfigurationHandlerBuildItem> configurationHandlerBuildItems,
BuildProducer<DevServicesNativeConfigResultBuildItem> devServicesResultBuildItemBuildProducer,
BuildProducer<ServiceStartBuildItem> serviceStartBuildItemBuildProducer) {
BuildProducer<DevServicesConfigResultBuildItem> devServicesResultBuildItemBuildProducer) {
//figure out if we need to shut down and restart existing databases
//if not and the DB's have already started we just return
if (databases != null) {
Expand All @@ -80,9 +74,6 @@ DevServicesDatasourceResultBuildItem launchDatabases(CurateOutcomeBuildItem cura
}
}
if (!restartRequired) {
for (RunTimeConfigurationDefaultBuildItem i : databaseConfig) {
runTimeConfigurationDefaultBuildItemBuildProducer.produce(i);
}
return null;
}
for (Closeable i : databases) {
Expand All @@ -94,7 +85,6 @@ DevServicesDatasourceResultBuildItem launchDatabases(CurateOutcomeBuildItem cura
}
databases = null;
cachedProperties = null;
databaseConfig = null;
}
DevServicesDatasourceResultBuildItem.DbResult defaultResult;
Map<String, DevServicesDatasourceResultBuildItem.DbResult> namedResults = new HashMap<>();
Expand Down Expand Up @@ -126,10 +116,10 @@ DevServicesDatasourceResultBuildItem launchDatabases(CurateOutcomeBuildItem cura
devDBProviderMap,
dataSourceBuildTimeConfig.defaultDataSource,
configHandlersByDbType, propertiesMap, closeableList, launchMode.getLaunchMode());
List<RunTimeConfigurationDefaultBuildItem> dbConfig = new ArrayList<>();
List<DevServicesConfigResultBuildItem> dbConfig = new ArrayList<>();
if (defaultResult != null) {
for (Map.Entry<String, String> i : defaultResult.getConfigProperties().entrySet()) {
dbConfig.add(new RunTimeConfigurationDefaultBuildItem(i.getKey(), i.getValue()));
dbConfig.add(new DevServicesConfigResultBuildItem(i.getKey(), i.getValue()));
}
}
for (Map.Entry<String, DataSourceBuildTimeConfig> entry : dataSourceBuildTimeConfig.namedDataSources.entrySet()) {
Expand All @@ -140,15 +130,14 @@ DevServicesDatasourceResultBuildItem launchDatabases(CurateOutcomeBuildItem cura
if (result != null) {
namedResults.put(entry.getKey(), result);
for (Map.Entry<String, String> i : result.getConfigProperties().entrySet()) {
dbConfig.add(new RunTimeConfigurationDefaultBuildItem(i.getKey(), i.getValue()));
dbConfig.add(new DevServicesConfigResultBuildItem(i.getKey(), i.getValue()));
}
}
}
for (RunTimeConfigurationDefaultBuildItem i : dbConfig) {
runTimeConfigurationDefaultBuildItemBuildProducer
for (DevServicesConfigResultBuildItem i : dbConfig) {
devServicesResultBuildItemBuildProducer
.produce(i);
}
databaseConfig = dbConfig;

if (first) {
first = false;
Expand Down Expand Up @@ -182,19 +171,6 @@ public void run() {
}
databases = closeableList;
cachedProperties = propertiesMap;

if (defaultResult != null) {
for (Map.Entry<String, String> entry : defaultResult.getConfigProperties().entrySet()) {
devServicesResultBuildItemBuildProducer
.produce(new DevServicesNativeConfigResultBuildItem(entry.getKey(), entry.getValue()));
}
}
for (DevServicesDatasourceResultBuildItem.DbResult i : namedResults.values()) {
for (Map.Entry<String, String> entry : i.getConfigProperties().entrySet()) {
devServicesResultBuildItemBuildProducer
.produce(new DevServicesNativeConfigResultBuildItem(entry.getKey(), entry.getValue()));
}
}
return new DevServicesDatasourceResultBuildItem(defaultResult, namedResults);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,9 @@
import io.quarkus.deployment.IsNormal;
import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.builditem.DevServicesNativeConfigResultBuildItem;
import io.quarkus.deployment.builditem.DevServicesConfigResultBuildItem;
import io.quarkus.deployment.builditem.DevServicesSharedNetworkBuildItem;
import io.quarkus.deployment.builditem.LaunchModeBuildItem;
import io.quarkus.deployment.builditem.RunTimeConfigurationDefaultBuildItem;
import io.quarkus.deployment.builditem.ServiceStartBuildItem;
import io.quarkus.devservices.common.ContainerAddress;
import io.quarkus.devservices.common.ContainerLocator;
import io.quarkus.runtime.LaunchMode;
Expand Down Expand Up @@ -77,17 +75,12 @@ public DevServicesKafkaBrokerBuildItem startKafkaDevService(
LaunchModeBuildItem launchMode,
KafkaBuildTimeConfig kafkaClientBuildTimeConfig,
Optional<DevServicesSharedNetworkBuildItem> devServicesSharedNetworkBuildItem,
BuildProducer<RunTimeConfigurationDefaultBuildItem> runTimeConfiguration,
BuildProducer<DevServicesNativeConfigResultBuildItem> devServicePropertiesProducer,
BuildProducer<ServiceStartBuildItem> serviceStartBuildItemBuildProducer) {
BuildProducer<DevServicesConfigResultBuildItem> devServicePropertiesProducer) {

KafkaDevServiceCfg configuration = getConfiguration(kafkaClientBuildTimeConfig);

if (closeable != null) {
boolean shouldShutdownTheBroker = launchMode.getLaunchMode() == LaunchMode.TEST;
if (!shouldShutdownTheBroker) {
shouldShutdownTheBroker = !configuration.equals(cfg);
}
boolean shouldShutdownTheBroker = !configuration.equals(cfg);
if (!shouldShutdownTheBroker) {
return null;
}
Expand All @@ -99,7 +92,7 @@ public DevServicesKafkaBrokerBuildItem startKafkaDevService(
DevServicesKafkaBrokerBuildItem bootstrapServers = null;
if (kafkaBroker != null) {
closeable = kafkaBroker.getCloseable();
runTimeConfiguration.produce(new RunTimeConfigurationDefaultBuildItem(
devServicePropertiesProducer.produce(new DevServicesConfigResultBuildItem(
KAFKA_BOOTSTRAP_SERVERS, kafkaBroker.getBootstrapServers()));
bootstrapServers = new DevServicesKafkaBrokerBuildItem(kafkaBroker.getBootstrapServers());
}
Expand Down Expand Up @@ -132,9 +125,6 @@ public DevServicesKafkaBrokerBuildItem startKafkaDevService(
bootstrapServers.getBootstrapServers());
}
createTopicPartitions(bootstrapServers.getBootstrapServers(), configuration);

devServicePropertiesProducer.produce(new DevServicesNativeConfigResultBuildItem("kafka.bootstrap.servers",
bootstrapServers.getBootstrapServers()));
}

return bootstrapServers;
Expand Down
Loading

0 comments on commit 64781be

Please sign in to comment.