From 5ffcee52bde5c8eb0f414e334d0db722ac725ab2 Mon Sep 17 00:00:00 2001 From: LE SAULNIER Kevin Date: Fri, 6 Dec 2024 11:59:12 +0100 Subject: [PATCH 1/6] feat: allow multiple es hosts Signed-off-by: LE SAULNIER Kevin --- .../caseserver/SupervisionController.java | 13 ++++--- .../caseserver/elasticsearch/ESConfig.java | 34 ++----------------- src/main/resources/application-local.yml | 14 ++++---- .../elasticsearch/EmbeddedElasticsearch.java | 3 +- 4 files changed, 20 insertions(+), 44 deletions(-) diff --git a/src/main/java/com/powsybl/caseserver/SupervisionController.java b/src/main/java/com/powsybl/caseserver/SupervisionController.java index 2f6cc95..68ce2d0 100644 --- a/src/main/java/com/powsybl/caseserver/SupervisionController.java +++ b/src/main/java/com/powsybl/caseserver/SupervisionController.java @@ -13,6 +13,8 @@ import io.swagger.v3.oas.annotations.responses.ApiResponse; import io.swagger.v3.oas.annotations.responses.ApiResponses; import io.swagger.v3.oas.annotations.tags.Tag; +import org.apache.http.HttpHost; +import org.elasticsearch.client.RestClient; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.data.elasticsearch.client.ClientConfiguration; @@ -31,13 +33,13 @@ public class SupervisionController { private final SupervisionService supervisionService; private final CaseService caseService; - private final ClientConfiguration elasticsearchClientConfiguration; + private final RestClient restClient; private final CaseInfosService caseInfosService; - public SupervisionController(SupervisionService supervisionService, CaseService caseService, ClientConfiguration elasticsearchClientConfiguration, CaseInfosService caseInfosService) { + public SupervisionController(SupervisionService supervisionService, CaseService caseService, RestClient restClient, CaseInfosService caseInfosService) { this.supervisionService = supervisionService; this.caseService = caseService; - this.elasticsearchClientConfiguration = elasticsearchClientConfiguration; + this.restClient = restClient; this.caseInfosService = caseInfosService; } @@ -45,9 +47,10 @@ public SupervisionController(SupervisionService supervisionService, CaseService @Operation(summary = "get the elasticsearch address") @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "the elasticsearch address")}) public ResponseEntity getElasticsearchHost() { - String host = elasticsearchClientConfiguration.getEndpoints().get(0).getHostName() + HttpHost httpHost = restClient.getNodes().get(0).getHost(); + String host = httpHost.getHostName() + ":" - + elasticsearchClientConfiguration.getEndpoints().get(0).getPort(); + + httpHost.getPort(); return ResponseEntity.ok().contentType(MediaType.TEXT_PLAIN).body(host); } diff --git a/src/main/java/com/powsybl/caseserver/elasticsearch/ESConfig.java b/src/main/java/com/powsybl/caseserver/elasticsearch/ESConfig.java index 1972b2e..d1539a3 100644 --- a/src/main/java/com/powsybl/caseserver/elasticsearch/ESConfig.java +++ b/src/main/java/com/powsybl/caseserver/elasticsearch/ESConfig.java @@ -7,6 +7,7 @@ package com.powsybl.caseserver.elasticsearch; import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchDataAutoConfiguration; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.convert.converter.Converter; @@ -14,6 +15,7 @@ import org.springframework.data.convert.WritingConverter; import org.springframework.data.elasticsearch.client.ClientConfiguration; import org.springframework.data.elasticsearch.client.elc.ElasticsearchConfiguration; +import org.springframework.data.elasticsearch.config.ElasticsearchConfigurationSupport; import org.springframework.data.elasticsearch.core.convert.ElasticsearchCustomConversions; import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories; @@ -30,40 +32,10 @@ @Configuration @EnableElasticsearchRepositories -public class ESConfig extends ElasticsearchConfiguration { +public class ESConfig extends ElasticsearchConfigurationSupport { public static final String CASE_INFOS_INDEX_NAME = "#{@environment.getProperty('powsybl-ws.elasticsearch.index.prefix')}cases"; - @Value("#{'${spring.data.elasticsearch.embedded:false}' ? 'localhost' : '${spring.data.elasticsearch.host}'}") - private String esHost; - - @Value("#{'${spring.data.elasticsearch.embedded:false}' ? '${spring.data.elasticsearch.embedded.port:}' : '${spring.data.elasticsearch.port}'}") - private int esPort; - - @Value("${spring.data.elasticsearch.client.timeout:60}") - private int timeout; - - @Value("${spring.data.elasticsearch.username:#{null}}") - private Optional username; - - @Value("${spring.data.elasticsearch.password:#{null}}") - private Optional password; - - //It should be detected without specifying the name, but it isn't. To investigate. - @Bean(name = "elasticsearchClientConfiguration") - @Override - public ClientConfiguration clientConfiguration() { - var clientConfiguration = ClientConfiguration.builder() - .connectedTo(esHost + ":" + esPort) - .withConnectTimeout(timeout * 1000L).withSocketTimeout(timeout * 1000L); - - if (username.isPresent() && password.isPresent()) { - clientConfiguration.withBasicAuth(username.get(), password.get()); - } - - return clientConfiguration.build(); - } - @Override public ElasticsearchCustomConversions elasticsearchCustomConversions() { return new ElasticsearchCustomConversions(Arrays.asList(DateToStringConverter.INSTANCE, StringToDateConverter.INSTANCE)); diff --git a/src/main/resources/application-local.yml b/src/main/resources/application-local.yml index 869d277..76cc000 100644 --- a/src/main/resources/application-local.yml +++ b/src/main/resources/application-local.yml @@ -5,12 +5,14 @@ spring: rabbitmq: addresses: localhost - data: - elasticsearch: - host: localhost - ## to fill if authentication is needed - # user: - # password: + elasticsearch: + uris: + - http://localhost:9200 + connection-timeout: 60s + socket-timeout: 60s + ## to fill if authentication is needed + # username: + # password: cloud: aws: endpoint: http://localhost:19000 diff --git a/src/test/java/com/powsybl/caseserver/elasticsearch/EmbeddedElasticsearch.java b/src/test/java/com/powsybl/caseserver/elasticsearch/EmbeddedElasticsearch.java index babeae8..b438ecf 100644 --- a/src/test/java/com/powsybl/caseserver/elasticsearch/EmbeddedElasticsearch.java +++ b/src/test/java/com/powsybl/caseserver/elasticsearch/EmbeddedElasticsearch.java @@ -41,8 +41,7 @@ public void postConstruct() { envMap.put("ES_JAVA_OPTS", "-Xms128m -Xmx128m"); envMap.put("action.auto_create_index", "false"); elasticsearchContainer.start(); - System.setProperty("spring.data.elasticsearch.embedded", Boolean.toString(true)); - System.setProperty("spring.data.elasticsearch.embedded.port", Integer.toString(elasticsearchContainer.getMappedPort(9200))); + System.setProperty("spring.elasticsearch.uris", "localhost:".concat(Integer.toString(elasticsearchContainer.getMappedPort(9200)))); } @PreDestroy From a2dd1b74a4a42843ccf235f374456a8579d8536a Mon Sep 17 00:00:00 2001 From: LE SAULNIER Kevin Date: Fri, 6 Dec 2024 15:15:55 +0100 Subject: [PATCH 2/6] fix: remove timeout, now handled in ws-commons Signed-off-by: LE SAULNIER Kevin --- src/main/resources/application-local.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main/resources/application-local.yml b/src/main/resources/application-local.yml index 76cc000..16f4d35 100644 --- a/src/main/resources/application-local.yml +++ b/src/main/resources/application-local.yml @@ -8,8 +8,6 @@ spring: elasticsearch: uris: - http://localhost:9200 - connection-timeout: 60s - socket-timeout: 60s ## to fill if authentication is needed # username: # password: From 62da979964a88929dbf07a2607352e9694b7daa7 Mon Sep 17 00:00:00 2001 From: LE SAULNIER Kevin Date: Tue, 10 Dec 2024 13:43:29 +0100 Subject: [PATCH 3/6] fix: checkstyle Signed-off-by: LE SAULNIER Kevin --- .../java/com/powsybl/caseserver/elasticsearch/ESConfig.java | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/main/java/com/powsybl/caseserver/elasticsearch/ESConfig.java b/src/main/java/com/powsybl/caseserver/elasticsearch/ESConfig.java index d1539a3..0d76602 100644 --- a/src/main/java/com/powsybl/caseserver/elasticsearch/ESConfig.java +++ b/src/main/java/com/powsybl/caseserver/elasticsearch/ESConfig.java @@ -6,15 +6,10 @@ */ package com.powsybl.caseserver.elasticsearch; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchDataAutoConfiguration; -import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.convert.converter.Converter; import org.springframework.data.convert.ReadingConverter; import org.springframework.data.convert.WritingConverter; -import org.springframework.data.elasticsearch.client.ClientConfiguration; -import org.springframework.data.elasticsearch.client.elc.ElasticsearchConfiguration; import org.springframework.data.elasticsearch.config.ElasticsearchConfigurationSupport; import org.springframework.data.elasticsearch.core.convert.ElasticsearchCustomConversions; import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories; @@ -22,7 +17,6 @@ import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import java.util.Arrays; -import java.util.Optional; /** * A class to configure DB elasticsearch client for metadatas transfer From 27f7233c037e08b2d1fd6041ce96370c1b80d759 Mon Sep 17 00:00:00 2001 From: LE SAULNIER Kevin Date: Thu, 12 Dec 2024 09:21:13 +0100 Subject: [PATCH 4/6] feat: upgrade ws-commons to 1.18.0 Signed-off-by: LE SAULNIER Kevin --- pom.xml | 1 + src/main/java/com/powsybl/caseserver/SupervisionController.java | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 3f398ee..65816cd 100644 --- a/pom.xml +++ b/pom.xml @@ -121,6 +121,7 @@ com.powsybl powsybl-ws-commons + 1.18.0 org.projectlombok diff --git a/src/main/java/com/powsybl/caseserver/SupervisionController.java b/src/main/java/com/powsybl/caseserver/SupervisionController.java index 68ce2d0..72e5a44 100644 --- a/src/main/java/com/powsybl/caseserver/SupervisionController.java +++ b/src/main/java/com/powsybl/caseserver/SupervisionController.java @@ -17,7 +17,6 @@ import org.elasticsearch.client.RestClient; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.data.elasticsearch.client.ClientConfiguration; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; From c05d06363fe1f7a583ee84d195034edfc50dad8c Mon Sep 17 00:00:00 2001 From: LE SAULNIER Kevin Date: Thu, 12 Dec 2024 13:15:47 +0100 Subject: [PATCH 5/6] fix: PR remark Signed-off-by: LE SAULNIER Kevin --- src/main/java/com/powsybl/caseserver/elasticsearch/ESConfig.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/com/powsybl/caseserver/elasticsearch/ESConfig.java b/src/main/java/com/powsybl/caseserver/elasticsearch/ESConfig.java index 0d76602..4c945dd 100644 --- a/src/main/java/com/powsybl/caseserver/elasticsearch/ESConfig.java +++ b/src/main/java/com/powsybl/caseserver/elasticsearch/ESConfig.java @@ -25,7 +25,6 @@ */ @Configuration -@EnableElasticsearchRepositories public class ESConfig extends ElasticsearchConfigurationSupport { public static final String CASE_INFOS_INDEX_NAME = "#{@environment.getProperty('powsybl-ws.elasticsearch.index.prefix')}cases"; From a70647cef6ffca0412efd18d7a19b6356f8e3701 Mon Sep 17 00:00:00 2001 From: LE SAULNIER Kevin Date: Thu, 12 Dec 2024 13:17:18 +0100 Subject: [PATCH 6/6] fix: checktyle Signed-off-by: LE SAULNIER Kevin --- src/main/java/com/powsybl/caseserver/elasticsearch/ESConfig.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/com/powsybl/caseserver/elasticsearch/ESConfig.java b/src/main/java/com/powsybl/caseserver/elasticsearch/ESConfig.java index 4c945dd..66f8bb6 100644 --- a/src/main/java/com/powsybl/caseserver/elasticsearch/ESConfig.java +++ b/src/main/java/com/powsybl/caseserver/elasticsearch/ESConfig.java @@ -12,7 +12,6 @@ import org.springframework.data.convert.WritingConverter; import org.springframework.data.elasticsearch.config.ElasticsearchConfigurationSupport; import org.springframework.data.elasticsearch.core.convert.ElasticsearchCustomConversions; -import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter;