From 96231d17d44e8246346fe7562394edae1fa4f7f2 Mon Sep 17 00:00:00 2001 From: Sebastian Marsching Date: Fri, 15 Mar 2024 18:20:35 +0100 Subject: [PATCH 01/16] Remove unused clusterName variable. --- src/main/java/org/phoebus/channelfinder/ElasticConfig.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main/java/org/phoebus/channelfinder/ElasticConfig.java b/src/main/java/org/phoebus/channelfinder/ElasticConfig.java index d2dd0c24..2e2ef6a0 100644 --- a/src/main/java/org/phoebus/channelfinder/ElasticConfig.java +++ b/src/main/java/org/phoebus/channelfinder/ElasticConfig.java @@ -62,8 +62,6 @@ public class ElasticConfig implements ServletContextListener { private ElasticsearchClient indexClient; private static final AtomicBoolean esInitialized = new AtomicBoolean(); - @Value("${elasticsearch.cluster.name:elasticsearch}") - private String clusterName; @Value("${elasticsearch.network.host:localhost}") private String host; @Value("${elasticsearch.http.port:9200}") From 2c493eb28f3cd23a0135dbe56b816867b669bb76 Mon Sep 17 00:00:00 2001 From: Sebastian Marsching Date: Fri, 15 Mar 2024 18:30:15 +0100 Subject: [PATCH 02/16] Avoid duplicate attempts to create indices. Before, esInitialized would be reset when createClient was called a second time. --- src/main/java/org/phoebus/channelfinder/ElasticConfig.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/org/phoebus/channelfinder/ElasticConfig.java b/src/main/java/org/phoebus/channelfinder/ElasticConfig.java index 2e2ef6a0..96804831 100644 --- a/src/main/java/org/phoebus/channelfinder/ElasticConfig.java +++ b/src/main/java/org/phoebus/channelfinder/ElasticConfig.java @@ -109,8 +109,7 @@ private static ElasticsearchClient createClient(ElasticsearchClient currentClien } else { client = currentClient; } - esInitialized.set(!Boolean.parseBoolean(createIndices)); - if (esInitialized.compareAndSet(false, true)) { + if (Boolean.parseBoolean(createIndices) && esInitialized.compareAndSet(false, true)) { config.elasticIndexValidation(client); } return client; From b64a57dec33d24a9bb59f106430863990443fab5 Mon Sep 17 00:00:00 2001 From: Sebastian Marsching Date: Fri, 15 Mar 2024 18:40:37 +0100 Subject: [PATCH 03/16] Allow specifying multiple Elasticsearch URLs. This enables the ChannelFinder to use continue working when one of the hosts in an Elasticsearch HA cluster goes down. By switching to URLs, it is also possible to connect to Elasticsearch through HTTPS instead of HTTP. --- .../phoebus/channelfinder/ElasticConfig.java | 38 ++++++++++++++++-- src/main/resources/application.properties | 40 ++++--------------- 2 files changed, 41 insertions(+), 37 deletions(-) diff --git a/src/main/java/org/phoebus/channelfinder/ElasticConfig.java b/src/main/java/org/phoebus/channelfinder/ElasticConfig.java index 96804831..8d01610a 100644 --- a/src/main/java/org/phoebus/channelfinder/ElasticConfig.java +++ b/src/main/java/org/phoebus/channelfinder/ElasticConfig.java @@ -17,6 +17,8 @@ import java.io.IOException; import java.io.InputStream; import java.text.MessageFormat; +import java.util.Arrays; +import java.util.List; import java.util.concurrent.atomic.AtomicBoolean; import java.util.logging.Level; import java.util.logging.Logger; @@ -64,6 +66,8 @@ public class ElasticConfig implements ServletContextListener { @Value("${elasticsearch.network.host:localhost}") private String host; + @Value("${elasticsearch.host_urls:http://localhost:9200}") + private String hostUrls; @Value("${elasticsearch.http.port:9200}") private int port; @Value("${elasticsearch.create.indices:true}") @@ -96,11 +100,15 @@ public int getES_QUERY_SIZE() { .addMixIn(Property.class, Property.OnlyProperty.class); private static ElasticsearchClient createClient(ElasticsearchClient currentClient, ObjectMapper objectMapper, - String host, int port, String createIndices, ElasticConfig config) { + List hostUrls, String createIndices, ElasticConfig config) { ElasticsearchClient client; if (currentClient == null) { // Create the low-level client - RestClient httpClient = RestClient.builder(new HttpHost(host, port)).build(); + HttpHost[] httpHosts = new HttpHost[hostUrls.size()]; + for (int i = 0; i < httpHosts.length; ++i) { + httpHosts[i] = HttpHost.create(hostUrls.get(i)); + } + RestClient httpClient = RestClient.builder(httpHosts).build(); // Create the Java API Client with the same low level client ElasticsearchTransport transport = new RestClientTransport(httpClient, new JacksonJsonpMapper(objectMapper)); @@ -115,15 +123,37 @@ private static ElasticsearchClient createClient(ElasticsearchClient currentClien return client; } + + private List getHostUrls() { + String hostUrls = this.hostUrls; + boolean hostIsDefault = (host == "localhost"); + boolean hostUrlsIsDefault = (hostUrls == "http://localhost:9200"); + boolean portIsDefault = (port == 9200); + if (hostUrlsIsDefault) { + if (!hostIsDefault || !portIsDefault) { + logger.warning("Specifying elasticsearch.network.host and elasticsearch.http.port is deprecated, please consider using elasticsearch.host_urls instead."); + hostUrls = "http://" + host + ":" + port; + } + } else { + if (!hostIsDefault) { + logger.warning("Only one of elasticsearch.host_urls and elasticsearch.network.host can be set, ignoring elasticsearch.network.host."); + } + if (!portIsDefault) { + logger.warning("Only one of elasticsearch.host_urls and elasticsearch.http.port can be set, ignoring elasticsearch.http.port."); + } + } + return Arrays.asList(hostUrls.split(",")); + } + @Bean({ "searchClient" }) public ElasticsearchClient getSearchClient() { - searchClient = createClient(searchClient, objectMapper, host, port, createIndices, this); + searchClient = createClient(searchClient, objectMapper, getHostUrls(), createIndices, this); return searchClient; } @Bean({ "indexClient" }) public ElasticsearchClient getIndexClient() { - indexClient = createClient(indexClient, objectMapper, host, port, createIndices, this); + indexClient = createClient(indexClient, objectMapper, getHostUrls(), createIndices, this); return indexClient; } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index f692013f..66bbe91e 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -65,44 +65,18 @@ tag-groups=cf-tags,USER ############################## Elastic Network And HTTP ############################### -# Elasticsearch, by default, binds itself to the 0.0.0.0 address, and listens -# on port [9200-9300] for HTTP traffic and on port [9300-9400] for node-to-node -# communication. (the range means that if the port is busy, it will automatically -# try the next port). +# Comma-separated list of URLs for the Elasticsearch hosts. All hosts listed +# here must belong to the same Elasticsearch cluster. +elasticsearch.host_urls: http://localhost:9200 -# Set the bind address specifically (IPv4 or IPv6): -# -# network.bind_host: 169.254.42.56 - -# Set the address other nodes will use to communicate with this node. If not -# set, it is automatically derived. It must point to an actual IP address. -# -# network.publish_host: 192.168.0.1 - -# Set both 'bind_host' and 'publish_host': -# +# Old way of configuring the Elasticsearch host. Deprecated in favor of +# elasticsearch.host_urls. elasticsearch.network.host: localhost -# Set a custom port for the node to node communication (9300 by default): -# -#elasticsearch.transport.tcp.port: 9300 - -# Enable compression for all communication between nodes (disabled by default): -# -#transport.tcp.compress: true - -# Set a custom port to listen for HTTP traffic: -# +# Old way of configuring the Elasticsearch HTTP port. Deprecated in favor of +# elasticsearch.host_urls. elasticsearch.http.port: 9200 -# Set a custom allowed content length: -# -#http.max_content_length: 100mb - -# Disable HTTP completely: -# -#http.enabled: false - # Elasticsearch index names and types used by channelfinder, ensure that any changes here should be replicated in the mapping_definitions.sh elasticsearch.tag.index = cf_tags elasticsearch.property.index = cf_properties From 103802fa2888fd11c84dc39df818df296bf03e84 Mon Sep 17 00:00:00 2001 From: Sebastian Marsching Date: Fri, 15 Mar 2024 19:09:23 +0100 Subject: [PATCH 04/16] Allow authentication with the Elasticsearch cluster. --- .../phoebus/channelfinder/ElasticConfig.java | 33 ++++++++++++++++++- src/main/resources/application.properties | 17 ++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/phoebus/channelfinder/ElasticConfig.java b/src/main/java/org/phoebus/channelfinder/ElasticConfig.java index 8d01610a..18fa7ad4 100644 --- a/src/main/java/org/phoebus/channelfinder/ElasticConfig.java +++ b/src/main/java/org/phoebus/channelfinder/ElasticConfig.java @@ -31,8 +31,16 @@ import co.elastic.clients.elasticsearch.indices.ExistsRequest; import co.elastic.clients.transport.endpoints.BooleanResponse; import com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.http.Header; import org.apache.http.HttpHost; +import org.apache.http.auth.AuthScope; +import org.apache.http.auth.UsernamePasswordCredentials; +import org.apache.http.client.CredentialsProvider; +import org.apache.http.impl.client.BasicCredentialsProvider; +import org.apache.http.impl.nio.client.HttpAsyncClientBuilder; +import org.apache.http.message.BasicHeader; import org.elasticsearch.client.RestClient; +import org.elasticsearch.client.RestClientBuilder; import org.phoebus.channelfinder.entity.Property; import org.phoebus.channelfinder.entity.Tag; import org.springframework.beans.factory.annotation.Value; @@ -70,6 +78,12 @@ public class ElasticConfig implements ServletContextListener { private String hostUrls; @Value("${elasticsearch.http.port:9200}") private int port; + @Value("${elasticsearch.authorization.header:}") + private String authorizationHeader; + @Value("${elasticsearch.authorization.username:}") + private String username; + @Value("${elasticsearch.authorization.password:}") + private String password; @Value("${elasticsearch.create.indices:true}") private String createIndices; @@ -108,7 +122,24 @@ private static ElasticsearchClient createClient(ElasticsearchClient currentClien for (int i = 0; i < httpHosts.length; ++i) { httpHosts[i] = HttpHost.create(hostUrls.get(i)); } - RestClient httpClient = RestClient.builder(httpHosts).build(); + RestClientBuilder clientBuilder = RestClient.builder(httpHosts); + // Configure authentication + if (!config.authorizationHeader.isEmpty()) { + clientBuilder.setDefaultHeaders(new Header[] {new BasicHeader("Authorization", config.authorizationHeader)}); + if (!config.username.isEmpty() || !config.password.isEmpty()) { + logger.warning("elasticsearch.authorization_header is set, ignoring elasticsearch.usernamd and elasticsearch.password."); + } + } else if (!config.username.isEmpty() || !config.password.isEmpty()) { + final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); + credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(config.username, config.password)); + clientBuilder.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() { + @Override + public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) { + return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider); + } + }); + } + RestClient httpClient = clientBuilder.build(); // Create the Java API Client with the same low level client ElasticsearchTransport transport = new RestClientTransport(httpClient, new JacksonJsonpMapper(objectMapper)); diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 66bbe91e..4bcedac4 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -77,6 +77,23 @@ elasticsearch.network.host: localhost # elasticsearch.host_urls. elasticsearch.http.port: 9200 +# Value of the Authorization header that is sent with requests to the +# Elasticsearch sever. This can be used for authentication using tokens or API +# keys. +# +# For example, for token authentication, set this to ?Bearer abcd1234?, where +# ?abcd1234? is the token. For API key authentication, set this to the Base64 +# encoded version of the concatenation of the API key ID and the API key +# secret, separated by a colon. See +# https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/8.12/_other_authentication_methods.html +# for details. +elasticsearch.authorization.header = + +# Username and password for authentication with the Elasticsearch server. This +# is only used if elasticsearch.authorization.header is not set. +elasticsearch.authorization.username = +elasticsearch.authorization.password = + # Elasticsearch index names and types used by channelfinder, ensure that any changes here should be replicated in the mapping_definitions.sh elasticsearch.tag.index = cf_tags elasticsearch.property.index = cf_properties From 529772fa1facf448b27b973de593243af2406f88 Mon Sep 17 00:00:00 2001 From: Sebastian Marsching Date: Mon, 18 Mar 2024 18:33:16 +0100 Subject: [PATCH 05/16] Use equals for string comparison. --- src/main/java/org/phoebus/channelfinder/ElasticConfig.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/phoebus/channelfinder/ElasticConfig.java b/src/main/java/org/phoebus/channelfinder/ElasticConfig.java index 18fa7ad4..0b21181a 100644 --- a/src/main/java/org/phoebus/channelfinder/ElasticConfig.java +++ b/src/main/java/org/phoebus/channelfinder/ElasticConfig.java @@ -157,8 +157,8 @@ public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpCli private List getHostUrls() { String hostUrls = this.hostUrls; - boolean hostIsDefault = (host == "localhost"); - boolean hostUrlsIsDefault = (hostUrls == "http://localhost:9200"); + boolean hostIsDefault = (host.equals("localhost")); + boolean hostUrlsIsDefault = (hostUrls.equals("http://localhost:9200")); boolean portIsDefault = (port == 9200); if (hostUrlsIsDefault) { if (!hostIsDefault || !portIsDefault) { From 7b7ed275c2fe74cb24f7b41a9dae7865b901c301 Mon Sep 17 00:00:00 2001 From: Sebastian Marsching Date: Mon, 18 Mar 2024 18:33:58 +0100 Subject: [PATCH 06/16] Avoid hiding class field with local variable. --- .../java/org/phoebus/channelfinder/ElasticConfig.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/phoebus/channelfinder/ElasticConfig.java b/src/main/java/org/phoebus/channelfinder/ElasticConfig.java index 0b21181a..04048b39 100644 --- a/src/main/java/org/phoebus/channelfinder/ElasticConfig.java +++ b/src/main/java/org/phoebus/channelfinder/ElasticConfig.java @@ -156,14 +156,14 @@ public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpCli } private List getHostUrls() { - String hostUrls = this.hostUrls; + String localHostUrls = this.hostUrls; boolean hostIsDefault = (host.equals("localhost")); - boolean hostUrlsIsDefault = (hostUrls.equals("http://localhost:9200")); + boolean hostUrlsIsDefault = (localHostUrls.equals("http://localhost:9200")); boolean portIsDefault = (port == 9200); if (hostUrlsIsDefault) { if (!hostIsDefault || !portIsDefault) { logger.warning("Specifying elasticsearch.network.host and elasticsearch.http.port is deprecated, please consider using elasticsearch.host_urls instead."); - hostUrls = "http://" + host + ":" + port; + localHostUrls = "http://" + host + ":" + port; } } else { if (!hostIsDefault) { @@ -173,7 +173,7 @@ private List getHostUrls() { logger.warning("Only one of elasticsearch.host_urls and elasticsearch.http.port can be set, ignoring elasticsearch.http.port."); } } - return Arrays.asList(hostUrls.split(",")); + return Arrays.asList(localHostUrls.split(",")); } @Bean({ "searchClient" }) From ca73e5ee3d9e8144f4f9725fe5ddbd090e2bc471 Mon Sep 17 00:00:00 2001 From: Sebastian Marsching Date: Mon, 18 Mar 2024 18:52:44 +0100 Subject: [PATCH 07/16] Remove unnecessary parentheses. --- src/main/java/org/phoebus/channelfinder/ElasticConfig.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/phoebus/channelfinder/ElasticConfig.java b/src/main/java/org/phoebus/channelfinder/ElasticConfig.java index 04048b39..8fecf791 100644 --- a/src/main/java/org/phoebus/channelfinder/ElasticConfig.java +++ b/src/main/java/org/phoebus/channelfinder/ElasticConfig.java @@ -157,8 +157,8 @@ public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpCli private List getHostUrls() { String localHostUrls = this.hostUrls; - boolean hostIsDefault = (host.equals("localhost")); - boolean hostUrlsIsDefault = (localHostUrls.equals("http://localhost:9200")); + boolean hostIsDefault = host.equals("localhost"); + boolean hostUrlsIsDefault = localHostUrls.equals("http://localhost:9200"); boolean portIsDefault = (port == 9200); if (hostUrlsIsDefault) { if (!hostIsDefault || !portIsDefault) { From 992c8828fccf8ae3d8d9f544ac970442032f6be2 Mon Sep 17 00:00:00 2001 From: Sebastian Marsching Date: Fri, 22 Mar 2024 16:58:30 +0100 Subject: [PATCH 08/16] Use array for ES host URLs. --- .../phoebus/channelfinder/ElasticConfig.java | 24 +++++++------------ 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/src/main/java/org/phoebus/channelfinder/ElasticConfig.java b/src/main/java/org/phoebus/channelfinder/ElasticConfig.java index 8fecf791..dfbdb320 100644 --- a/src/main/java/org/phoebus/channelfinder/ElasticConfig.java +++ b/src/main/java/org/phoebus/channelfinder/ElasticConfig.java @@ -17,8 +17,6 @@ import java.io.IOException; import java.io.InputStream; import java.text.MessageFormat; -import java.util.Arrays; -import java.util.List; import java.util.concurrent.atomic.AtomicBoolean; import java.util.logging.Level; import java.util.logging.Logger; @@ -75,7 +73,7 @@ public class ElasticConfig implements ServletContextListener { @Value("${elasticsearch.network.host:localhost}") private String host; @Value("${elasticsearch.host_urls:http://localhost:9200}") - private String hostUrls; + private HttpHost[] httpHosts; @Value("${elasticsearch.http.port:9200}") private int port; @Value("${elasticsearch.authorization.header:}") @@ -114,14 +112,10 @@ public int getES_QUERY_SIZE() { .addMixIn(Property.class, Property.OnlyProperty.class); private static ElasticsearchClient createClient(ElasticsearchClient currentClient, ObjectMapper objectMapper, - List hostUrls, String createIndices, ElasticConfig config) { + HttpHost[] httpHosts, String createIndices, ElasticConfig config) { ElasticsearchClient client; if (currentClient == null) { // Create the low-level client - HttpHost[] httpHosts = new HttpHost[hostUrls.size()]; - for (int i = 0; i < httpHosts.length; ++i) { - httpHosts[i] = HttpHost.create(hostUrls.get(i)); - } RestClientBuilder clientBuilder = RestClient.builder(httpHosts); // Configure authentication if (!config.authorizationHeader.isEmpty()) { @@ -155,15 +149,15 @@ public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpCli } - private List getHostUrls() { - String localHostUrls = this.hostUrls; + private HttpHost[] getHttpHosts() { + HttpHost[] localHttpHosts = this.httpHosts; boolean hostIsDefault = host.equals("localhost"); - boolean hostUrlsIsDefault = localHostUrls.equals("http://localhost:9200"); + boolean hostUrlsIsDefault = localHttpHosts.length == 1 && localHttpHosts[0].equals(new HttpHost("http://localhost:9200")); boolean portIsDefault = (port == 9200); if (hostUrlsIsDefault) { if (!hostIsDefault || !portIsDefault) { logger.warning("Specifying elasticsearch.network.host and elasticsearch.http.port is deprecated, please consider using elasticsearch.host_urls instead."); - localHostUrls = "http://" + host + ":" + port; + localHttpHosts = new HttpHost[] {new HttpHost("http://" + host + ":" + port)}; } } else { if (!hostIsDefault) { @@ -173,18 +167,18 @@ private List getHostUrls() { logger.warning("Only one of elasticsearch.host_urls and elasticsearch.http.port can be set, ignoring elasticsearch.http.port."); } } - return Arrays.asList(localHostUrls.split(",")); + return localHttpHosts; } @Bean({ "searchClient" }) public ElasticsearchClient getSearchClient() { - searchClient = createClient(searchClient, objectMapper, getHostUrls(), createIndices, this); + searchClient = createClient(searchClient, objectMapper, getHttpHosts(), createIndices, this); return searchClient; } @Bean({ "indexClient" }) public ElasticsearchClient getIndexClient() { - indexClient = createClient(indexClient, objectMapper, getHostUrls(), createIndices, this); + indexClient = createClient(indexClient, objectMapper, getHttpHosts(), createIndices, this); return indexClient; } From a78b24b105f32d37a2c8a8d03dc40a59109b33f4 Mon Sep 17 00:00:00 2001 From: Sebastian Marsching Date: Fri, 22 Mar 2024 17:11:00 +0100 Subject: [PATCH 09/16] Fix typo in log message. --- src/main/java/org/phoebus/channelfinder/ElasticConfig.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/phoebus/channelfinder/ElasticConfig.java b/src/main/java/org/phoebus/channelfinder/ElasticConfig.java index dfbdb320..538ac697 100644 --- a/src/main/java/org/phoebus/channelfinder/ElasticConfig.java +++ b/src/main/java/org/phoebus/channelfinder/ElasticConfig.java @@ -121,7 +121,7 @@ private static ElasticsearchClient createClient(ElasticsearchClient currentClien if (!config.authorizationHeader.isEmpty()) { clientBuilder.setDefaultHeaders(new Header[] {new BasicHeader("Authorization", config.authorizationHeader)}); if (!config.username.isEmpty() || !config.password.isEmpty()) { - logger.warning("elasticsearch.authorization_header is set, ignoring elasticsearch.usernamd and elasticsearch.password."); + logger.warning("elasticsearch.authorization_header is set, ignoring elasticsearch.username and elasticsearch.password."); } } else if (!config.username.isEmpty() || !config.password.isEmpty()) { final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); From 7a03a31587cac694695e5b3d376fc5741385059d Mon Sep 17 00:00:00 2001 From: Sebastian Marsching Date: Fri, 22 Mar 2024 17:12:46 +0100 Subject: [PATCH 10/16] Use lambda expression instead of anonymous class. --- src/main/java/org/phoebus/channelfinder/ElasticConfig.java | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/main/java/org/phoebus/channelfinder/ElasticConfig.java b/src/main/java/org/phoebus/channelfinder/ElasticConfig.java index 538ac697..c747260a 100644 --- a/src/main/java/org/phoebus/channelfinder/ElasticConfig.java +++ b/src/main/java/org/phoebus/channelfinder/ElasticConfig.java @@ -126,12 +126,7 @@ private static ElasticsearchClient createClient(ElasticsearchClient currentClien } else if (!config.username.isEmpty() || !config.password.isEmpty()) { final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(config.username, config.password)); - clientBuilder.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() { - @Override - public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) { - return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider); - } - }); + clientBuilder.setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider)); } RestClient httpClient = clientBuilder.build(); From 49548d5c39e11df4ecf33fc91ca2bbae31c64b0f Mon Sep 17 00:00:00 2001 From: Sebastian Marsching Date: Fri, 22 Mar 2024 17:45:24 +0100 Subject: [PATCH 11/16] Use elasticsearch.host_urls property in test configuration. --- .../resources/application_test.properties | 40 ++----------------- 1 file changed, 3 insertions(+), 37 deletions(-) diff --git a/src/test/resources/application_test.properties b/src/test/resources/application_test.properties index 78b60d53..b02bb97b 100644 --- a/src/test/resources/application_test.properties +++ b/src/test/resources/application_test.properties @@ -61,43 +61,9 @@ tag-groups=cf-tags ############################## Elastic Network And HTTP ############################### -# Elasticsearch, by default, binds itself to the 0.0.0.0 address, and listens -# on port [9200-9300] for HTTP traffic and on port [9300-9400] for node-to-node -# communication. (the range means that if the port is busy, it will automatically -# try the next port). - -# Set the bind address specifically (IPv4 or IPv6): -# -# network.bind_host: 169.254.42.56 - -# Set the address other nodes will use to communicate with this node. If not -# set, it is automatically derived. It must point to an actual IP address. -# -# network.publish_host: 192.168.0.1 - -# Set both 'bind_host' and 'publish_host': -# -elasticsearch.network.host: localhost - -# Set a custom port for the node to node communication (9300 by default): -# -#elasticsearch.transport.tcp.port: 9300 - -# Enable compression for all communication between nodes (disabled by default): -# -#transport.tcp.compress: true - -# Set a custom port to listen for HTTP traffic: -# -elasticsearch.http.port: 9200 - -# Set a custom allowed content length: -# -#http.max_content_length: 100mb - -# Disable HTTP completely: -# -#http.enabled: false +# Comma-separated list of URLs for the Elasticsearch hosts. All hosts listed +# here must belong to the same Elasticsearch cluster. +elasticsearch.host_urls=http://localhost:9200 # Elasticsearch index names and types used by channelfinder, ensure that any changes here should be replicated in the mapping_definitions.sh elasticsearch.tag.index = test_${random.int[1,1000]}_cf_tags From f49786b91e25165d8b9462ec87001fc48dcea039 Mon Sep 17 00:00:00 2001 From: Sebastian Marsching Date: Fri, 22 Mar 2024 17:47:32 +0100 Subject: [PATCH 12/16] Use equal sign instead of colon in properties files. --- src/main/resources/application.properties | 8 ++++---- src/test/resources/application_test.properties | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 4bcedac4..6b15becc 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -67,15 +67,15 @@ tag-groups=cf-tags,USER # Comma-separated list of URLs for the Elasticsearch hosts. All hosts listed # here must belong to the same Elasticsearch cluster. -elasticsearch.host_urls: http://localhost:9200 +elasticsearch.host_urls=http://localhost:9200 # Old way of configuring the Elasticsearch host. Deprecated in favor of # elasticsearch.host_urls. -elasticsearch.network.host: localhost +elasticsearch.network.host=localhost # Old way of configuring the Elasticsearch HTTP port. Deprecated in favor of # elasticsearch.host_urls. -elasticsearch.http.port: 9200 +elasticsearch.http.port=9200 # Value of the Authorization header that is sent with requests to the # Elasticsearch sever. This can be used for authentication using tokens or API @@ -103,7 +103,7 @@ elasticsearch.channel.index = channelfinder elasticsearch.query.size = 10000 # Create the Channel Finder indices if they do not exist -elasticsearch.create.indices: true +elasticsearch.create.indices=true ############################## Service Info ############################### # ChannelFinder version as defined in the pom file diff --git a/src/test/resources/application_test.properties b/src/test/resources/application_test.properties index b02bb97b..2297af05 100644 --- a/src/test/resources/application_test.properties +++ b/src/test/resources/application_test.properties @@ -74,7 +74,7 @@ elasticsearch.channel.index = test_${random.int[1,1000]}_channelfinder elasticsearch.query.size = 10000 # Create the Channel Finder indices if they do not exist -elasticsearch.create.indices: true +elasticsearch.create.indices = true ############################## Service Info ############################### # ChannelFinder version as defined in the pom file From b10db0cc1c56be451c0a6a709a3eebb32d6a1b0e Mon Sep 17 00:00:00 2001 From: Sebastian Marsching Date: Mon, 25 Mar 2024 16:53:39 +0100 Subject: [PATCH 13/16] Use HttpHost.create instead of HttpHost::new. The latter one expects a hostname instead of a URL. --- .../org/phoebus/channelfinder/ElasticConfig.java | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/phoebus/channelfinder/ElasticConfig.java b/src/main/java/org/phoebus/channelfinder/ElasticConfig.java index c747260a..ba0d390f 100644 --- a/src/main/java/org/phoebus/channelfinder/ElasticConfig.java +++ b/src/main/java/org/phoebus/channelfinder/ElasticConfig.java @@ -17,6 +17,7 @@ import java.io.IOException; import java.io.InputStream; import java.text.MessageFormat; +import java.util.Arrays; import java.util.concurrent.atomic.AtomicBoolean; import java.util.logging.Level; import java.util.logging.Logger; @@ -73,7 +74,7 @@ public class ElasticConfig implements ServletContextListener { @Value("${elasticsearch.network.host:localhost}") private String host; @Value("${elasticsearch.host_urls:http://localhost:9200}") - private HttpHost[] httpHosts; + private String[] httpHostUrls; @Value("${elasticsearch.http.port:9200}") private int port; @Value("${elasticsearch.authorization.header:}") @@ -145,15 +146,12 @@ private static ElasticsearchClient createClient(ElasticsearchClient currentClien } private HttpHost[] getHttpHosts() { - HttpHost[] localHttpHosts = this.httpHosts; boolean hostIsDefault = host.equals("localhost"); - boolean hostUrlsIsDefault = localHttpHosts.length == 1 && localHttpHosts[0].equals(new HttpHost("http://localhost:9200")); + boolean hostUrlsIsDefault = httpHostUrls.length == 1 && httpHostUrls[0].equals("http://localhost:9200"); boolean portIsDefault = (port == 9200); - if (hostUrlsIsDefault) { - if (!hostIsDefault || !portIsDefault) { - logger.warning("Specifying elasticsearch.network.host and elasticsearch.http.port is deprecated, please consider using elasticsearch.host_urls instead."); - localHttpHosts = new HttpHost[] {new HttpHost("http://" + host + ":" + port)}; - } + if (hostUrlsIsDefault && (!hostIsDefault || !portIsDefault)) { + logger.warning("Specifying elasticsearch.network.host and elasticsearch.http.port is deprecated, please consider using elasticsearch.host_urls instead."); + return new HttpHost[] {new HttpHost(host, port)}; } else { if (!hostIsDefault) { logger.warning("Only one of elasticsearch.host_urls and elasticsearch.network.host can be set, ignoring elasticsearch.network.host."); @@ -161,8 +159,8 @@ private HttpHost[] getHttpHosts() { if (!portIsDefault) { logger.warning("Only one of elasticsearch.host_urls and elasticsearch.http.port can be set, ignoring elasticsearch.http.port."); } + return Arrays.stream(httpHostUrls).map(HttpHost::create).toArray(HttpHost[]::new); } - return localHttpHosts; } @Bean({ "searchClient" }) From 19236ffdf0b55145e8f4ede3c9ac95f0f982e248 Mon Sep 17 00:00:00 2001 From: Sebastian Marsching Date: Thu, 28 Mar 2024 13:31:50 +0100 Subject: [PATCH 14/16] Use version 2.16.0 of jackson-core. Using a version of jackson-core that does not match jackson-databind causes problems (see #127 for details). --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 81567db6..b53d6b9c 100644 --- a/pom.xml +++ b/pom.xml @@ -110,7 +110,7 @@ com.fasterxml.jackson.core jackson-core - 2.14.2 + 2.16.0 jakarta.json From 67d1fff89375eea13462905ce9da0d0a35043d4a Mon Sep 17 00:00:00 2001 From: Sebastian Marsching Date: Thu, 28 Mar 2024 14:06:06 +0100 Subject: [PATCH 15/16] Revert changes to index validation logic. --- src/main/java/org/phoebus/channelfinder/ElasticConfig.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/phoebus/channelfinder/ElasticConfig.java b/src/main/java/org/phoebus/channelfinder/ElasticConfig.java index ba0d390f..d56a4c4c 100644 --- a/src/main/java/org/phoebus/channelfinder/ElasticConfig.java +++ b/src/main/java/org/phoebus/channelfinder/ElasticConfig.java @@ -138,7 +138,8 @@ private static ElasticsearchClient createClient(ElasticsearchClient currentClien } else { client = currentClient; } - if (Boolean.parseBoolean(createIndices) && esInitialized.compareAndSet(false, true)) { + esInitialized.set(!Boolean.parseBoolean(createIndices)); + if (esInitialized.compareAndSet(false, true)) { config.elasticIndexValidation(client); } return client; From 2674f1933ec532ff5732924e97f79b0a2b96d11c Mon Sep 17 00:00:00 2001 From: Sebastian Marsching Date: Thu, 28 Mar 2024 14:26:43 +0100 Subject: [PATCH 16/16] Remove esInitialized flag. --- src/main/java/org/phoebus/channelfinder/ElasticConfig.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/main/java/org/phoebus/channelfinder/ElasticConfig.java b/src/main/java/org/phoebus/channelfinder/ElasticConfig.java index d56a4c4c..f20a1204 100644 --- a/src/main/java/org/phoebus/channelfinder/ElasticConfig.java +++ b/src/main/java/org/phoebus/channelfinder/ElasticConfig.java @@ -69,7 +69,6 @@ public class ElasticConfig implements ServletContextListener { private ElasticsearchClient searchClient; private ElasticsearchClient indexClient; - private static final AtomicBoolean esInitialized = new AtomicBoolean(); @Value("${elasticsearch.network.host:localhost}") private String host; @@ -138,8 +137,7 @@ private static ElasticsearchClient createClient(ElasticsearchClient currentClien } else { client = currentClient; } - esInitialized.set(!Boolean.parseBoolean(createIndices)); - if (esInitialized.compareAndSet(false, true)) { + if (Boolean.parseBoolean(createIndices)) { config.elasticIndexValidation(client); } return client;