Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow multiple elasticsearch hosts #54

Merged
merged 8 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@
<dependency>
<groupId>com.powsybl</groupId>
<artifactId>powsybl-ws-commons</artifactId>
<version>1.18.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
Expand Down
14 changes: 8 additions & 6 deletions src/main/java/com/powsybl/caseserver/SupervisionController.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
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;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
Expand All @@ -31,23 +32,24 @@ 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;
}

@GetMapping(value = "/elasticsearch-host")
@Operation(summary = "get the elasticsearch address")
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "the elasticsearch address")})
public ResponseEntity<String> 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);
}

Expand Down
40 changes: 2 additions & 38 deletions src/main/java/com/powsybl/caseserver/elasticsearch/ESConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,16 @@
*/
package com.powsybl.caseserver.elasticsearch;

import org.springframework.beans.factory.annotation.Value;
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;

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
Expand All @@ -29,41 +24,10 @@
*/

@Configuration
@EnableElasticsearchRepositories
public class ESConfig extends ElasticsearchConfiguration {
public class ESConfig extends ElasticsearchConfigurationSupport {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice to have this 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<String> username;

@Value("${spring.data.elasticsearch.password:#{null}}")
private Optional<String> 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));
Expand Down
12 changes: 6 additions & 6 deletions src/main/resources/application-local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ spring:
rabbitmq:
addresses: localhost

data:
elasticsearch:
host: localhost
## to fill if authentication is needed
# user:
# password:
elasticsearch:
uris:
- http://localhost:9200
## to fill if authentication is needed
# username:
# password:
cloud:
aws:
endpoint: http://localhost:19000
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading