From 92391bdc60daa5a9fd423f0383761170d00e1916 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yoann=20Rodi=C3=A8re?= Date: Fri, 22 Sep 2023 15:14:41 +0200 Subject: [PATCH] Switch Elasticsearch extensions to @ConfigMapping --- .../lowlevel/runtime/ElasticsearchConfig.java | 52 +++++++++---------- .../ElasticsearchRestClientProducer.java | 2 +- .../runtime/RestClientBuilderHelper.java | 30 +++++------ 3 files changed, 41 insertions(+), 43 deletions(-) diff --git a/extensions/elasticsearch-rest-client/runtime/src/main/java/io/quarkus/elasticsearch/restclient/lowlevel/runtime/ElasticsearchConfig.java b/extensions/elasticsearch-rest-client/runtime/src/main/java/io/quarkus/elasticsearch/restclient/lowlevel/runtime/ElasticsearchConfig.java index 6933ca9e283dc..6b4ccf0fc04dd 100644 --- a/extensions/elasticsearch-rest-client/runtime/src/main/java/io/quarkus/elasticsearch/restclient/lowlevel/runtime/ElasticsearchConfig.java +++ b/extensions/elasticsearch-rest-client/runtime/src/main/java/io/quarkus/elasticsearch/restclient/lowlevel/runtime/ElasticsearchConfig.java @@ -6,61 +6,61 @@ import java.util.Optional; import io.quarkus.runtime.annotations.ConfigGroup; -import io.quarkus.runtime.annotations.ConfigItem; import io.quarkus.runtime.annotations.ConfigPhase; import io.quarkus.runtime.annotations.ConfigRoot; +import io.smallrye.config.ConfigMapping; +import io.smallrye.config.WithDefault; +@ConfigMapping(prefix = "quarkus.elasticsearch") @ConfigRoot(phase = ConfigPhase.RUN_TIME) -public class ElasticsearchConfig { +public interface ElasticsearchConfig { /** * The list of hosts of the Elasticsearch servers. */ - @ConfigItem(defaultValue = "localhost:9200") - public List hosts; + @WithDefault("localhost:9200") + List hosts(); /** * The protocol to use when contacting Elasticsearch servers. * Set to "https" to enable SSL/TLS. */ - @ConfigItem(defaultValue = "http") - public String protocol; + @WithDefault("http") + String protocol(); /** * The username for basic HTTP authentication. */ - @ConfigItem - public Optional username; + Optional username(); /** * The password for basic HTTP authentication. */ - @ConfigItem - public Optional password; + Optional password(); /** * The connection timeout. */ - @ConfigItem(defaultValue = "1S") - public Duration connectionTimeout; + @WithDefault("1S") + Duration connectionTimeout(); /** * The socket timeout. */ - @ConfigItem(defaultValue = "30S") - public Duration socketTimeout; + @WithDefault("30S") + Duration socketTimeout(); /** * The maximum number of connections to all the Elasticsearch servers. */ - @ConfigItem(defaultValue = "20") - public int maxConnections; + @WithDefault("20") + int maxConnections(); /** * The maximum number of connections per Elasticsearch server. */ - @ConfigItem(defaultValue = "10") - public int maxConnectionsPerRoute; + @WithDefault("10") + int maxConnectionsPerRoute(); /** * The number of IO thread. @@ -72,28 +72,26 @@ public class ElasticsearchConfig { * @see * number of threads */ - @ConfigItem - public Optional ioThreadCounts; + Optional ioThreadCounts(); /** * Configuration for the automatic discovery of new Elasticsearch nodes. */ - @ConfigItem - public DiscoveryConfig discovery; + DiscoveryConfig discovery(); @ConfigGroup - public static class DiscoveryConfig { + interface DiscoveryConfig { /** * Defines if automatic discovery is enabled. */ - @ConfigItem(defaultValue = "false") - public boolean enabled; + @WithDefault("false") + boolean enabled(); /** * Refresh interval of the node list. */ - @ConfigItem(defaultValue = "5M") - public Duration refreshInterval; + @WithDefault("5M") + Duration refreshInterval(); } } diff --git a/extensions/elasticsearch-rest-client/runtime/src/main/java/io/quarkus/elasticsearch/restclient/lowlevel/runtime/ElasticsearchRestClientProducer.java b/extensions/elasticsearch-rest-client/runtime/src/main/java/io/quarkus/elasticsearch/restclient/lowlevel/runtime/ElasticsearchRestClientProducer.java index bbe796b9051b1..d4a89b74dae52 100644 --- a/extensions/elasticsearch-rest-client/runtime/src/main/java/io/quarkus/elasticsearch/restclient/lowlevel/runtime/ElasticsearchRestClientProducer.java +++ b/extensions/elasticsearch-rest-client/runtime/src/main/java/io/quarkus/elasticsearch/restclient/lowlevel/runtime/ElasticsearchRestClientProducer.java @@ -29,7 +29,7 @@ public RestClient restClient() { RestClientBuilder builder = RestClientBuilderHelper.createRestClientBuilder(config); this.client = builder.build(); - if (config.discovery.enabled) { + if (config.discovery().enabled()) { this.sniffer = RestClientBuilderHelper.createSniffer(client, config); } diff --git a/extensions/elasticsearch-rest-client/runtime/src/main/java/io/quarkus/elasticsearch/restclient/lowlevel/runtime/RestClientBuilderHelper.java b/extensions/elasticsearch-rest-client/runtime/src/main/java/io/quarkus/elasticsearch/restclient/lowlevel/runtime/RestClientBuilderHelper.java index 4f64f2da31919..f17e348c1ea81 100644 --- a/extensions/elasticsearch-rest-client/runtime/src/main/java/io/quarkus/elasticsearch/restclient/lowlevel/runtime/RestClientBuilderHelper.java +++ b/extensions/elasticsearch-rest-client/runtime/src/main/java/io/quarkus/elasticsearch/restclient/lowlevel/runtime/RestClientBuilderHelper.java @@ -34,9 +34,9 @@ private RestClientBuilderHelper() { } public static RestClientBuilder createRestClientBuilder(ElasticsearchConfig config) { - List hosts = new ArrayList<>(config.hosts.size()); - for (InetSocketAddress host : config.hosts) { - hosts.add(new HttpHost(host.getHostString(), host.getPort(), config.protocol)); + List hosts = new ArrayList<>(config.hosts().size()); + for (InetSocketAddress host : config.hosts()) { + hosts.add(new HttpHost(host.getHostString(), host.getPort(), config.protocol())); } RestClientBuilder builder = RestClient.builder(hosts.toArray(new HttpHost[0])); @@ -45,8 +45,8 @@ public static RestClientBuilder createRestClientBuilder(ElasticsearchConfig conf @Override public RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder requestConfigBuilder) { return requestConfigBuilder - .setConnectTimeout((int) config.connectionTimeout.toMillis()) - .setSocketTimeout((int) config.socketTimeout.toMillis()) + .setConnectTimeout((int) config.connectionTimeout().toMillis()) + .setSocketTimeout((int) config.socketTimeout().toMillis()) .setConnectionRequestTimeout(0); // Avoid requests being flagged as timed out even when they didn't time out. } }); @@ -54,28 +54,28 @@ public RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder reques builder.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() { @Override public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) { - if (config.username.isPresent()) { - if (!"https".equalsIgnoreCase(config.protocol)) { + if (config.username().isPresent()) { + if (!"https".equalsIgnoreCase(config.protocol())) { LOG.warn("Using Basic authentication in HTTP implies sending plain text passwords over the wire, " + "use the HTTPS protocol instead."); } CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, - new UsernamePasswordCredentials(config.username.get(), config.password.orElse(null))); + new UsernamePasswordCredentials(config.username().get(), config.password().orElse(null))); httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider); } - if (config.ioThreadCounts.isPresent()) { + if (config.ioThreadCounts().isPresent()) { IOReactorConfig ioReactorConfig = IOReactorConfig.custom() - .setIoThreadCount(config.ioThreadCounts.get()) + .setIoThreadCount(config.ioThreadCounts().get()) .build(); httpClientBuilder.setDefaultIOReactorConfig(ioReactorConfig); } - httpClientBuilder.setMaxConnTotal(config.maxConnections); - httpClientBuilder.setMaxConnPerRoute(config.maxConnectionsPerRoute); + httpClientBuilder.setMaxConnTotal(config.maxConnections()); + httpClientBuilder.setMaxConnPerRoute(config.maxConnectionsPerRoute()); - if ("http".equalsIgnoreCase(config.protocol)) { + if ("http".equalsIgnoreCase(config.protocol())) { // In this case disable the SSL capability as it might have an impact on // bootstrap time, for example consuming entropy for no reason httpClientBuilder.setSSLStrategy(NoopIOSessionStrategy.INSTANCE); @@ -99,10 +99,10 @@ public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpCli public static Sniffer createSniffer(RestClient client, ElasticsearchConfig config) { SnifferBuilder builder = Sniffer.builder(client) - .setSniffIntervalMillis((int) config.discovery.refreshInterval.toMillis()); + .setSniffIntervalMillis((int) config.discovery().refreshInterval().toMillis()); // https discovery support - if ("https".equalsIgnoreCase(config.protocol)) { + if ("https".equalsIgnoreCase(config.protocol())) { NodesSniffer hostsSniffer = new ElasticsearchNodesSniffer( client, ElasticsearchNodesSniffer.DEFAULT_SNIFF_REQUEST_TIMEOUT, // 1sec