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

Parallelize merge processes #18

Open
wants to merge 44 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
2219ea0
parallelize merges first version
ChamseddineBhd Sep 23, 2020
0be48ef
config poolSize
ChamseddineBhd Sep 23, 2020
bc4cb4f
some enhancments
ChamseddineBhd Sep 23, 2020
b7502ac
change default value thread timeout
ChamseddineBhd Sep 23, 2020
46dd9ba
code smell
ChamseddineBhd Sep 23, 2020
546019f
use single executer serverice
ChamseddineBhd Sep 25, 2020
45ddaee
Merge branch 'master' into parallelize-merge-processes
ChamseddineBhd Sep 25, 2020
103e10c
use reactor for asynchronous calls
ChamseddineBhd Sep 25, 2020
bda7dd6
load flow asynchronous call
ChamseddineBhd Sep 25, 2020
9670a98
Merge branch 'parallelize-merge-processes' of https://github.com/grid…
ChamseddineBhd Sep 25, 2020
5f8aecd
asynchronous http calls
ChamseddineBhd Sep 27, 2020
114c380
Merge branch 'master' into parallelize-merge-processes
ChamseddineBhd Sep 29, 2020
ca798fc
fix code smells
ChamseddineBhd Sep 29, 2020
5419d2c
Merge branch 'parallelize-merge-processes' of https://github.com/grid…
ChamseddineBhd Sep 29, 2020
e3c6e04
add balanceComputationParamsFile param
ChamseddineBhd Sep 29, 2020
895bd27
code smell
ChamseddineBhd Sep 29, 2020
bf86b06
remove log for validMono
ChamseddineBhd Sep 29, 2020
000b4c4
non blacking from callable
ChamseddineBhd Oct 5, 2020
d069036
remove useless comment
ChamseddineBhd Oct 5, 2020
6147d63
Merge branch 'master' into parallelize-merge-processes
ChamseddineBhd Oct 9, 2020
bca2900
Merge branch 'master' into parallelize-merge-processes
ChamseddineBhd Oct 20, 2020
835880e
review 1/2
ChamseddineBhd Oct 20, 2020
aaa4729
review 2/2
ChamseddineBhd Oct 20, 2020
57f550f
make merge reactive
ChamseddineBhd Oct 20, 2020
7c8a1f5
fixes
ChamseddineBhd Oct 20, 2020
997ece5
remove useless header
ChamseddineBhd Oct 21, 2020
1c1f403
checkstyle fixes
ChamseddineBhd Oct 21, 2020
05c7eff
code smell
ChamseddineBhd Oct 21, 2020
edbf6b8
test correction
ChamseddineBhd Oct 25, 2020
e2cd341
Merge branch 'master' into parallelize-merge-processes
ChamseddineBhd Oct 25, 2020
256b822
Merge branch 'master' into parallelize-merge-processes
ChamseddineBhd Nov 6, 2020
6de5c90
jon review
ChamseddineBhd Nov 8, 2020
8b5853f
remove useless subsribe
ChamseddineBhd Nov 10, 2020
73144c8
refactoring
ChamseddineBhd Nov 12, 2020
a840c30
doOnSuccess instead of flatMap
ChamseddineBhd Nov 12, 2020
d169829
cosmetic change
ChamseddineBhd Nov 30, 2020
7d09d17
use then() instead of collectList()
ChamseddineBhd Dec 1, 2020
aee5fb6
resolve conflict in pom.xml
ChamseddineBhd Dec 10, 2020
cea7bef
add test //
ChamseddineBhd Dec 10, 2020
7991ed8
test fixes
ChamseddineBhd Dec 10, 2020
01bf5b8
cosmetic changes
ChamseddineBhd Dec 10, 2020
0b58e31
checkstyle
ChamseddineBhd Dec 10, 2020
c5a77c3
rename parameter
ChamseddineBhd Dec 16, 2020
5e42b10
remove redundant dependency
ChamseddineBhd Jan 4, 2021
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
29 changes: 29 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@
<java.version>11</java.version>

<gridsuite-dependencies.version>4</gridsuite-dependencies.version>

<mockwebserver.version>4.0.1</mockwebserver.version>
<reactor.test.version>3.2.3.RELEASE</reactor.test.version>
</properties>

<build>
Expand Down Expand Up @@ -89,6 +92,14 @@

<dependencies>
<!-- Compilation dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-core</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
Expand Down Expand Up @@ -159,6 +170,24 @@
</dependency>

<!-- Test dependencies -->
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>mockwebserver</artifactId>
<version>${mockwebserver.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
<version>${reactor.test.version}</version>
</dependency>

<dependency>
<groupId>com.github.nosan</groupId>
<artifactId>embedded-cassandra-spring-test</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,17 @@
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.ResourceUtils;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.DefaultUriBuilderFactory;
import org.springframework.web.util.UriComponentsBuilder;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

import static org.gridsuite.merge.orchestrator.server.ServicesUtils.DELIMITER;

/**
* @author Jon Harper <jon.harper at rte-france.com>
Expand All @@ -37,47 +36,38 @@
public class BalancesAdjustmentService {

private static final String BALANCE_ADJUSTEMENT_API_VERSION = "v1";
private static final String DELIMITER = "/";

private RestTemplate balancesAdjustmentServerRest;
private WebClient webClient;

@Autowired
public BalancesAdjustmentService(RestTemplateBuilder builder,
@Value("${backing-services.balances-adjustment-server.base-uri:http://balances-adjustment-server/}") String balanceAdjustementBaseUri) {
this.balancesAdjustmentServerRest = builder.uriTemplateHandler(
new DefaultUriBuilderFactory(balanceAdjustementBaseUri)
).build();
@Value("${backing-services.balances-adjustment-server.base-uri:http://balances-adjustment-server/}") String balanceAdjustementBaseUri,
WebClient.Builder webClientBuilder) {
this.webClient = webClientBuilder.baseUrl(balanceAdjustementBaseUri).build();
}

public BalancesAdjustmentService(RestTemplate restTemplate) {
this.balancesAdjustmentServerRest = restTemplate;
public BalancesAdjustmentService(String loadFlowBaseUri) {
WebClient.Builder webClientBuilder = WebClient.builder();
this.webClient = webClientBuilder.baseUrl(loadFlowBaseUri).build();
}

public String doBalance(List<UUID> networksIds) {
public Mono<String> doBalance(List<UUID> networksIds) {
try {
File targetNetPositionsFile = ResourceUtils.getFile("classpath:targetNetPositions.json");

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);

MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
body.add("balanceComputationParamsFile", null);
body.add("balanceComputationParamsFile", "null");
body.add("targetNetPositionFile", new FileSystemResource(targetNetPositionsFile));

HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);
String uri = ServicesUtils.getStringUri(networksIds, DELIMITER, BALANCE_ADJUSTEMENT_API_VERSION);

UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromPath(DELIMITER + BALANCE_ADJUSTEMENT_API_VERSION + "/networks/{networkUuid}/run");
for (int i = 1; i < networksIds.size(); ++i) {
uriBuilder = uriBuilder.queryParam("networkUuid", networksIds.get(i).toString());
}
String uri = uriBuilder.build().toUriString();
return webClient.put()
.uri(uri)
.header(HttpHeaders.CONTENT_TYPE, String.valueOf(MediaType.MULTIPART_FORM_DATA))
.body(BodyInserters.fromValue(body))
.retrieve()
.bodyToMono(String.class);

ResponseEntity<String> res = balancesAdjustmentServerRest.exchange(uri,
HttpMethod.PUT,
requestEntity,
String.class,
networksIds.get(0).toString());
return res.getBody();
} catch (FileNotFoundException e) {
throw new PowsyblException("No target net positions file found");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
import org.springframework.web.util.DefaultUriBuilderFactory;

import org.springframework.web.util.UriComponentsBuilder;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;

/**
* @author Jon Harper <jon.harper at rte-france.com>
Expand Down Expand Up @@ -107,9 +109,11 @@ public List<CaseInfos> getCases(List<String> tsos, ZonedDateTime dateTime, Strin
return Collections.emptyList();
}

public UUID importCase(UUID caseUuid) {
CaseDataSourceClient dataSource = new CaseDataSourceClient(caseServerRest, caseUuid);
Network network = networkStoreService.importNetwork(dataSource);
return networkStoreService.getNetworkUuid(network);
public Mono<UUID> importCase(UUID caseUuid) {
return Mono.fromCallable(() -> {
CaseDataSourceClient dataSource = new CaseDataSourceClient(caseServerRest, caseUuid);
Network network = networkStoreService.importNetwork(dataSource);
return networkStoreService.getNetworkUuid(network);
}).subscribeOn(Schedulers.boundedElastic());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@
import com.powsybl.commons.PowsyblException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.DefaultUriBuilderFactory;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.util.UriComponentsBuilder;
import reactor.core.publisher.Mono;

import java.util.UUID;

Expand All @@ -29,33 +27,39 @@ public class IgmQualityCheckService {

private static final String CASE_VALIDATION_API_VERSION = "v1";

private RestTemplate caseValidationServerRest;
private WebClient webClient;

@Autowired
public IgmQualityCheckService(RestTemplateBuilder builder,
@Value("${backing-services.case-validation-server.base-uri:http://case-validation-server/}") String caseValidationBaseUri) {
this.caseValidationServerRest = builder.uriTemplateHandler(new DefaultUriBuilderFactory(caseValidationBaseUri)).build();
public IgmQualityCheckService(@Value("${backing-services.case-validation-server.base-uri:http://case-validation-server/}") String caseValidationBaseUri,
WebClient.Builder webClientBuilder) {
this.webClient = webClientBuilder.baseUrl(caseValidationBaseUri).build();
}

public IgmQualityCheckService(RestTemplate restTemplate) {
this.caseValidationServerRest = restTemplate;
public IgmQualityCheckService(WebClient webClient) {
this.webClient = webClient;
}

public boolean check(UUID networkUuid) {
boolean res = false;
try {
ResponseEntity<String> response = caseValidationServerRest.exchange(CASE_VALIDATION_API_VERSION + "/networks/{networkUuid}/validate",
HttpMethod.PUT,
null,
String.class,
networkUuid.toString());
JsonNode node = new ObjectMapper().readTree(response.getBody()).path("loadFlowOk");
public Mono<Boolean> check(UUID networkUuid) {
UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromPath(CASE_VALIDATION_API_VERSION + "/networks/{networkUuid}/validate");
String uri = uriBuilder.buildAndExpand(networkUuid.toString()).toUriString();

Mono<String> stringMono = webClient.put()
.uri(uri)
.retrieve()
.bodyToMono(String.class);

return stringMono.map(str -> {
boolean res = false;
JsonNode node = null;
try {
node = new ObjectMapper().readTree(str).path("loadFlowOk");
} catch (JsonProcessingException e) {
throw new PowsyblException("Error parsing case validation result");
}
if (!node.isMissingNode()) {
res = node.asBoolean();
}
} catch (JsonProcessingException e) {
throw new PowsyblException("Error parsing case validation result");
}
return res;
return res;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,11 @@
*/
package org.gridsuite.merge.orchestrator.server;

import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.DefaultUriBuilderFactory;
import org.springframework.web.util.UriComponentsBuilder;
import reactor.core.publisher.Mono;

import java.util.List;
import java.util.UUID;
Expand All @@ -28,32 +24,24 @@ public class LoadFlowService {
private static final String LOAD_FLOW_API_VERSION = "v1";
private static final String DELIMITER = "/";

private RestTemplate loadFlowServerRest;
private WebClient webClient;

@Autowired
public LoadFlowService(RestTemplateBuilder builder,
@Value("${backing-services.loadflow-server.base-uri:http://loadflow-server/}") String loadFlowBaseUri) {
this.loadFlowServerRest = builder.uriTemplateHandler(
new DefaultUriBuilderFactory(loadFlowBaseUri)
).build();
public LoadFlowService(@Value("${backing-services.loadflow-server.base-uri:http://loadflow-server/}") String loadFlowBaseUri,
WebClient.Builder webClientBuilder) {
this.webClient = webClientBuilder.baseUrl(loadFlowBaseUri).build();
}

public LoadFlowService(RestTemplate restTemplate) {
this.loadFlowServerRest = restTemplate;
public LoadFlowService(String loadFlowBaseUri) {
WebClient.Builder webClientBuilder = WebClient.builder();
this.webClient = webClientBuilder.baseUrl(loadFlowBaseUri).build();
}

public String run(List<UUID> networksIds) {
UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromPath(DELIMITER + LOAD_FLOW_API_VERSION + "/networks/{networkUuid}/run");
for (int i = 1; i < networksIds.size(); ++i) {
uriBuilder = uriBuilder.queryParam("networkUuid", networksIds.get(i).toString());
}
String uri = uriBuilder.build().toUriString();

ResponseEntity<String> res = loadFlowServerRest.exchange(uri,
HttpMethod.PUT,
null,
String.class,
networksIds.get(0).toString());
return res.getBody();
public Mono<String> run(List<UUID> networksIds) {
String uri = ServicesUtils.getStringUri(networksIds, DELIMITER, LOAD_FLOW_API_VERSION);
return webClient.put()
.uri(uri)
.retrieve()
.bodyToMono(String.class);
}
}
Loading