Skip to content

Commit

Permalink
Switch Elasticsearch extensions to @ConfigMapping
Browse files Browse the repository at this point in the history
  • Loading branch information
yrodiere committed Sep 22, 2023
1 parent d98090e commit 92391bd
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<InetSocketAddress> hosts;
@WithDefault("localhost:9200")
List<InetSocketAddress> 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<String> username;
Optional<String> username();

/**
* The password for basic HTTP authentication.
*/
@ConfigItem
public Optional<String> password;
Optional<String> 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.
Expand All @@ -72,28 +72,26 @@ public class ElasticsearchConfig {
* @see <a href="https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/current/_number_of_threads.html">
* number of threads</a>
*/
@ConfigItem
public Optional<Integer> ioThreadCounts;
Optional<Integer> 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ private RestClientBuilderHelper() {
}

public static RestClientBuilder createRestClientBuilder(ElasticsearchConfig config) {
List<HttpHost> hosts = new ArrayList<>(config.hosts.size());
for (InetSocketAddress host : config.hosts) {
hosts.add(new HttpHost(host.getHostString(), host.getPort(), config.protocol));
List<HttpHost> 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]));
Expand All @@ -45,37 +45,37 @@ 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.
}
});

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);
Expand All @@ -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
Expand Down

0 comments on commit 92391bd

Please sign in to comment.